Jump to content

How to get parent class's method call from child?


saumya

Recommended Posts

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

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();

 

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();

 

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.