saumya Posted June 28, 2007 Share Posted June 28, 2007 hi, I believe PHP deals with classes the same way as other OOP programming languages.But surprisingly it does not, I hope. Here is a situation I came across, I have 2 classes "Controller" and "Controller_login" where class Controller_login extends Controller There is a method in "Controller" named "getModel()", which I want to call from "Controller_login". To my surprise, if I call it directly as "getModel()", PHP throws an error. To solve this I am calling the method as parent::getModel(); which solves my problem. In a general OOP environment this should not have happened?!! I am concluding that PHP has got this peculiar behaviour, so one has to live with it. Please suggest whether my understanding is correct ? thank you Link to comment https://forums.phpfreaks.com/topic/57526-how-to-get-parent-classs-method-call-from-child/ Share on other sites More sharing options...
KrisNz Posted June 28, 2007 Share Posted June 28, 2007 did you try $this->getModel() ?? Calling a function in a class with php is done with either $this->functionOrFunctionInParent(); parent::functionInParent(); self::functionWithinClass(); className::staticFunction(); $instance->function(); Link to comment https://forums.phpfreaks.com/topic/57526-how-to-get-parent-classs-method-call-from-child/#findComment-284680 Share on other sites More sharing options...
saumya Posted June 28, 2007 Author Share Posted June 28, 2007 hi KrisNz, tahks for the response. I have not tried $this->getModel(); I think thats the thing I was missing. Previously I was a little confused as in other languages we just call method name, if we are calling from inside the same class and I have tried the same thing in my script and failed.I will keep in mind that if one has to call a method it has to be this->method(); not method(); Link to comment https://forums.phpfreaks.com/topic/57526-how-to-get-parent-classs-method-call-from-child/#findComment-284709 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.