PHP 4: does method_exist return true if parent class method even if child class forgot to call parent::

This is a discussion on PHP 4: does method_exist return true if parent class method even if child class forgot to call parent:: within the PHP Language forums, part of the PHP Programming Forums category; My code was dying on the line below where I use method_exists: if (class_exists($nameOfClassToBeUsed)) { $object = new $nameOfClassToBeUsed(); $this->...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-28-2005
lkrubner@geocities.com
 
Posts: n/a
Default PHP 4: does method_exist return true if parent class method even if child class forgot to call parent::

My code was dying on the line below where I use method_exists:

if (class_exists($nameOfClassToBeUsed)) {
$object = new $nameOfClassToBeUsed();
$this->arrayOfAllTheObjectsSoFarLoaded[$nameOfClassToBeUsed] = &
$object;
if (method_exists($object, "setCallingCode"))
$object->setCallingCode($nameOfFunctionOrClassCalling);
return $object;
} else {
$this->error("$nameOfClassToBeUsed not found. The code that wants
this class is $nameOfFunctionOrClassCalling ");
}



The method is in the parent class yet I'd forgotten to call parent:: in
the constructor of the child class. It seems to me that method_exists
was testing true, though the method was not truly available, and then
the code died. Adding parent:: to the constructor of the child solved
the problem. Is this a bug in PHP?

Reply With Quote
  #2 (permalink)  
Old 05-28-2005
Jacob Atzen
 
Posts: n/a
Default Re: PHP 4: does method_exist return true if parent class method even if child class forgot to call parent::

On 2005-05-28, lkrubner@geocities.com <lkrubner@geocities.com> wrote:
> The method is in the parent class yet I'd forgotten to call parent::
> in the constructor of the child class. It seems to me that
> method_exists was testing true, though the method was not truly
> available, and then the code died. Adding parent:: to the constructor
> of the child solved the problem. Is this a bug in PHP?


You shouldn't have to call the parent class to get access to its
methods. If class B extends class A then all methods of class A is
available in class B.

--
Cheers,
- Jacob Atzen
Reply With Quote
  #3 (permalink)  
Old 05-28-2005
Wayne
 
Posts: n/a
Default Re: PHP 4: does method_exist return true if parent class method even if child class forgot to call parent::

On 28 May 2005 07:03:45 -0700, lkrubner@geocities.com wrote:

>Adding parent:: to the constructor of the child solved
>the problem. Is this a bug in PHP?


Nope.

>The method is in the parent class yet I'd forgotten to call parent:: in
>the constructor of the child class.


That's correct behaviour. Calling the parents constructor has nothing
to do with whether the methods are available in a child class.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:48 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0