johnwarde Posted April 18, 2007 Share Posted April 18, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/47568-using-an-oject-reference-directly-return-from-a-function-call/ Share on other sites More sharing options...
utexas_pjm Posted April 18, 2007 Share Posted April 18, 2007 I'm not so sure that is possible in loosely typed language. I had a hard time with with that realization coming from a Java background. Quote Link to comment https://forums.phpfreaks.com/topic/47568-using-an-oject-reference-directly-return-from-a-function-call/#findComment-232306 Share on other sites More sharing options...
Daniel0 Posted April 18, 2007 Share Posted April 18, 2007 That isn't possible in PHP. The object must be assigned to a variable first. Quote Link to comment https://forums.phpfreaks.com/topic/47568-using-an-oject-reference-directly-return-from-a-function-call/#findComment-232635 Share on other sites More sharing options...
Jenk Posted April 19, 2007 Share Posted April 19, 2007 It's only possible in PHP5+. The above people are only correct if they are referring to php4. Quote Link to comment https://forums.phpfreaks.com/topic/47568-using-an-oject-reference-directly-return-from-a-function-call/#findComment-233633 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.