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

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

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

Post by saumya »

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

Code: Select all

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

Code: Select all

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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

class Controller_login extends Controller 
{
   public function __construct()
   {
      print_r($this->getModel());
   }
}

class Controller
{
   protected function getModel()
   {
      return array('foo' => 'bar');
   }
}

$controller = new Controller_login(); 

//outputs Array ( [foo] => bar )
Not sure what you mean?
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

hi Jcart,
thanks for the reply.
I will try your method.
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

Code: Select all

this->method();
not

Code: Select all

method();
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

In PHP: "$this->method()" and not "this->method()" :wink:
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

ohh yes, missed again.
But no way, only

Code: Select all

method();
always has to be

Code: Select all

$this->method();
thanks
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post by kyberfabrikken »

Yes, in Java, the $this-> part is implicit. That isn't possible in PHP, because then you wouldn't be able to distinguish between global functions and member methods. Eg. the following would be ambiguous:

Code: Select all

function foo() {
}
class Bar
{
  function foo() {
  }
  function doStuff() {
    foo(); // does this refer to $this->foo() or the global function foo() ?
  }
}
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

ohh yeah.That clears it all.
thank you so much guys.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

kyberfabrikken wrote:Yes, in Java, the $this-> part is implicit. That isn't possible in PHP, because then you wouldn't be able to distinguish between global functions and member methods.
Well, that's not a problem in most languages: look for the method first, if not found, take a global function, that's simple. On the other side, this syntax may look confusing for beginners, perhaps this is the reason why it wasn't implemented.
Post Reply