I solved it but thanks...
for those who might be interested (the correct function):
function EngineClass(&$engine){
$this->engine=&$engine;
}
Hi all, please take a look at this code and tell me why it is not working...
what I try to do here is to make an Engine, and I want to pass a reference of it to all objects created in Engine.
I included the output also, and as you can see instead of what I want (pass the reference of Engine to all classes), the classes get a copy of the current Engine class... but why ??????????? please help !!!
class Engine{
var $wannaLog=false;
var $logger=false;
function main(){
$this->wannaLog=new WannaLog($this);
$this->logger=new Logger($this);
$this->wannaLog->doit();
}
}
class EngineClass{
var $engine=false;
function EngineClass(&$engine){
$this->engine=$engine;
}
function logEngine($msg){
if(is_object($this->engine->logger)){
$this->engine->logger->logEngine(get_class($this) . $msg);
}
}
}
class Logger extends EngineClass{
function logEngine($msg){
echo $msg;
}
}
class WannaLog extends EngineClass{
function doit(){
$this->logEngine("wanna log");
}
}
$engine=new Engine();
$engine->main();
echo "<pre>\n\n";
echo var_dump($engine);
echo "</pre>\n\n";
OUTPUT:
object(engine)(2) {
["wannaLog"]=>
&object(wannalog)(1) {
["engine"]=>
object(engine)(2) {
["wannaLog"]=>
bool(false)
["logger"]=>
bool(false)
}
}
["logger"]=>
object(logger)(1) {
["engine"]=>
object(engine)(2) {
["wannaLog"]=>
object(wannalog)(1) {
["engine"]=>
object(engine)(2) {
["wannaLog"]=>
bool(false)
["logger"]=>
bool(false)
}
}
["logger"]=>
bool(false)
}
}
}