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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lkrubner@geocities.com

    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($ nameOfClassToBe Used)) {
    $object = new $nameOfClassToB eUsed();
    $this->arrayOfAllTheO bjectsSoFarLoad ed[$nameOfClassToB eUsed] = &
    $object;
    if (method_exists( $object, "setCallingCode "))
    $object->setCallingCode ($nameOfFunctio nOrClassCalling );
    return $object;
    } else {
    $this->error("$nameOf ClassToBeUsed not found. The code that wants
    this class is $nameOfFunction OrClassCalling ");
    }



    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?

  • Jacob Atzen

    #2
    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@geocit ies.com <lkrubner@geoci ties.com> wrote:[color=blue]
    > 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?[/color]

    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

    Comment

    • Wayne

      #3
      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@geocit ies.com wrote:
      [color=blue]
      >Adding parent:: to the constructor of the child solved
      >the problem. Is this a bug in PHP?[/color]

      Nope.
      [color=blue]
      >The method is in the parent class yet I'd forgotten to call parent:: in
      >the constructor of the child class.[/color]

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

      Comment

      Working...