purencool Posted November 18, 2011 Share Posted November 18, 2011 hi php people, I am wondering will if the code below will work when I call $C->getafoo() will I get foo; I keep getting an error at this point return $this->A->a(); saying the method is not there class A{ public function a(){ return "foo"; } } class B { public $A; function __construct($A) { $this->A = $A; } public function geta(){ return $this->A->a(); } } class C extends B{ public function getafoo(){ return geta(); } } $C = new C(new A()); echo $C->getafoo(); Quote Link to comment https://forums.phpfreaks.com/topic/251363-oop-question/ Share on other sites More sharing options...
Adam Posted November 18, 2011 Share Posted November 18, 2011 You need to call internal class methods using the special $this var: public function getafoo(){ return $this->geta(); } Quote Link to comment https://forums.phpfreaks.com/topic/251363-oop-question/#findComment-1289233 Share on other sites More sharing options...
purencool Posted November 18, 2011 Author Share Posted November 18, 2011 Thanks for the reply. Yes your right in the script I am testing I have got that but thanks for pointing it out I left it out of the example. What I am wonder can I do this $this->A->a(); in php? Quote Link to comment https://forums.phpfreaks.com/topic/251363-oop-question/#findComment-1289235 Share on other sites More sharing options...
trq Posted November 18, 2011 Share Posted November 18, 2011 Yes. Why don't you just try it? Quote Link to comment https://forums.phpfreaks.com/topic/251363-oop-question/#findComment-1289238 Share on other sites More sharing options...
Adam Posted November 18, 2011 Share Posted November 18, 2011 Yup, as long as "A" is an object with a public method called "a()", there's no problem. Quote Link to comment https://forums.phpfreaks.com/topic/251363-oop-question/#findComment-1289240 Share on other sites More sharing options...
purencool Posted November 18, 2011 Author Share Posted November 18, 2011 thanks I have been testing I can't get to work what I have created a little more complicated but if the struck is right I have go back to testing thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/251363-oop-question/#findComment-1289244 Share on other sites More sharing options...
Adam Posted November 18, 2011 Share Posted November 18, 2011 What do you mean it doesn't work? It works in your example when you add $this to the geta() call..? Quote Link to comment https://forums.phpfreaks.com/topic/251363-oop-question/#findComment-1289246 Share on other sites More sharing options...
purencool Posted November 18, 2011 Author Share Posted November 18, 2011 yes the example works but I must have another issue driving the error. I was just confirming that the theory was right thanks again Quote Link to comment https://forums.phpfreaks.com/topic/251363-oop-question/#findComment-1289255 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.