Jump to content

Using an oject reference directly return from a function call


johnwarde

Recommended Posts

Hi,

 

I am trying to use an object reference directly from a function call (that returns the object reference):

echo $objTestB->getObj()->getVal();

 

See code for full example.

 

class TestA {
    var $_strTest = 'Hello World!';
    function getVal() {
        return $this->_strTest;
    }
}

class  TestB {
    var $_objTestA = NULL;
    function TestB() {
        $this->_objTestA = new TestA;
    }
    function &getObj() {
        return $this->_objTestA;
    }
}


$objTestB = new TestB;

$refTestA = $objTestB->getObj();
echo $refTestA->getVal();
// I know the above 2 lines work, but I want to do it in one line like this...
echo $objTestB->getObj()->getVal();

 

Thanks for your help,

 

JB  :)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.