|
php namespace |
Posted by Bob on Apr-23-2013 07:02 |
|
Is there an easy way to put ChartDirector into a namespace? I'm trying to put the
phpchartdir.php into the new PSR0 PHP standard (https://github.com/php-fig/fig-
standards/blob/master/accepted/PSR-0.md) to make it is easier to use with other
packages. When I separate each class from phpchartdir.php into it's own individual file and
add the following at the top of each file:
namespace ChartDirector;
require_once dirname(__FILE__) . '/functions.php'; (this includes all the functions and
constants defined in the phpchartdir.php file)
None of the classes can then call $this->ptr as it is not defined in the classes. If I add
public $ptr as a variable I end up with the error "Error converting object pointer to type
P9BaseChart". I'm somewhat confused how phpchartdir links the ptr into each object? My
goal is to end up with a ChartDirector/XYChart.php, ChartDirector/BaseChart.php, etc.
Regards |
Re: php namespace |
Posted by Peter Kwan on Apr-23-2013 12:59 |
|
Hi Bob,
ChartDirector for PHP currently does not use namespace because PHP namespace breaks backwards compatibility. If you would like to use namespace, you may modify a few locations in "phpchartdir.php".
(a) Add the line "namespace ChartDirector;" as the top of "phpchartdir.php" (just under the "<?php" line).
(b) Change
register_shutdown_function("garbageCollector");
to
register_shutdown_function("ChartDirector\\\\garbageCollector");
(c) Change all constructors to use __construct. For example, for the PieChart class, the first few lines are:
class PieChart extends BaseChart {
function PieChart(.....) {
....
}
....
Please change "function PieChart" to "function __construct".
The reason for the above change it because "as of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor." See:
http://php.net/manual/en/language.oop5.decon.php
For your case, I suspect you have not changed the constructor names. So the constructor is never called, and $this->ptr is never defined.
Hope this can help.
Regards
Peter Kwan |
|