views:

200

answers:

8

As the title states, I'm trying to make a method in a parent class required. Although, I suppose it could be any class. For instance:

class Parent
   {
      funtion foo ()
        {
           // do stuff
        }
   }

  class Child extends Parent
    {
       function bar ()
        {
           // do stuff after foo() has ran
        }
    }

Basically, I want foo() to be required to run or Child class doesn't run and returns an error or redirects to a different page. I could call the function, but I'm wondering If I can make it a requirement when extending the parent class.

A: 

Why not just set a boolean in function foo() that acts as a flag. Check to see if it has been set in the child class/functions, and you're all set.

Akoi Meexx
A: 

I think this might be the only way of implementing such functionality, as I don't think there is a built in solution.

class Parent
{
    public $foo_accessed = FALSE;
    funtion foo ()
    {
       $this->foo_accessed=TRUE;
       // do stuff
    }
}

class Child extends Parent
{
   function bar ()
    {
       if($this->foo_accessed==TRUE) {
           // do stuff after foo() has ran
       } else {
           // throw an error
       }
    }
}
ILMV
A: 

Have the child call the function from the parent in the construct.

class Child extends Parent
{
   function bar ()
    {
       // do stuff after foo() has ran
    }
    function __construct(){
         parent::foo();
    }
}
bucho
A: 

What you should probably do is override Parent::foo() and then call the parent method in the overridden method like so:

class Parent
{
  funtion foo ()
    {
       // do stuff
    }
}

class Child extends Parent
{
   function foo ()
    {
       if(!parent::foo()) {
      throw new Exception('Foo failed');
       }

       // do child class stuff
    }
}
Noah Goodrich
+9  A: 

If you leverage abstract classes and methods, you can force subclasses to implement the missing methods.

abstract class ParentClass
{
  public function foo ()
  {
    // do stuff
    $this->bar();
  }

  abstract protected function bar();
}

class Child extends ParentClass
{
  protected function bar()
  {
    // does stuff
  }
}

Subclasses that don't implement bar() will generate a fatal error.

Peter Bailey
Making foo() final would also protect it.
Tim Lytle
A: 

Do not depend on other methods. Make sure they've ran.

class Parent
{
    funtion foo()
   {
       // do stuff
   }
}

class Child extends Parent
{
    private function bar()
   {
       // do child class stuff
   }

   public function doFooBar()
   {
    parent::foo();
    $this->bar();
   }

}
erenon
A: 

As already mentioned, it sounds like you want foo() to be abstract, forcing child classes to override it.

Any class containing an abstract class in PHP requires your parent class to be abstract too. This means it can't be instantiated (constructed), only derived / sub-classed. If you try to instantiate an abstract class the compiler will issue a fatal error.

http://php.net/manual/en/language.oop5.abstract.php

See the code in Peter Bailey's answer.

Greg K
A: 

If you're not actually initializing any code within parent class you should use an object interface. Interface methods have to be implemented or the script will throw a fetal error.

More information on them can be found: http://us3.php.net/interface.

evolve