tags:

views:

79

answers:

1

See code:

class Foo {

    public static function dumpObj($obj) {

        var_dump($obj);

    }

}

class Bar {

    public $someData = 'bla';

    public function __construct() {

        Foo::dumpObj($this);

    }

}

new Bar;

I'm guessing this outputs 'null' because $this is special and we just can't do this sort of thing with it. As you can see, I want to call Bar's method from within the Foo class during Bar's inception, and need Bar's object to do so. I can of course modify Bar's object in __construct, so assumed the object has been created and something outside the Bar class could modify it too.

Is there another way to do this? Foo unfortunately has to remain static and separate of Bar.

Much thanks for any responses.

+3  A: 

This works for me, at least in PHP 5.2.8. It outputs this:

object(Bar)#1 (1) {
  ["someData"]=>
  string(3) "bla"
}
JW
Works for me too (tested on 5.1.6, 5.2.9, and 5.3.0)
Pascal MARTIN
Indeed it does work after all - thanks all for the fast responses. I had left an erroneous global declaration from a previous version in my actual code, causing my problem. So much for 2 hours spent in confusion!
tjbp