I'm not certain how to explain this with the correct terms so maybe an example is the best method...
$master = new MasterClass();
$master->doStuff();
class MasterClass {
var $a;
var $b;
var $c;
var $eventProccer;
开发者_如何学Python function MasterClass()
{
$this->a = 1;
$this->eventProccer = new EventProcess();
}
function printCurrent()
{
echo '<br>'.$this->a.'<br>';
}
function doStuff()
{
$this->printCurrent();
$this->eventProccer->DoSomething();
$this->printCurrent();
}
}
class EventProcess {
function EventProcess() {}
function DoSomething()
{
// trying to access and change the parent class' a,b,c properties
}
}
My problem is i'm not certain how to access the properties of the MasterClass from within the EventProcess->DoSomething() method? I would need to access, perform operations on and update the properties. The a,b,c properties will be quite large arrays and the DoSomething() method would be called many times during the execuction of the script. Any help or pointers would be much appreciated :)
This has been discussed a number of times on SO, every time with the result that there is no "native" way of doing this. You would have to pass a reference to the MasterClass
instance to the child class. In PHP5:
$this->eventProccer = new EventProcess($this);
and store it in the child class like so:
class EventProcess
{
private $masterClass; // Reference to master class
function __construct($masterClass)
{
$this->masterClass = $masterClass;
}
精彩评论