Search code examples
phpinheritanceterminate

terminate execution by parent method


I have the following code:

class A
{
   function example($bool)
   {
      echo "Bob";
      if($bool === true)
      {
         //how to terminate?
      }
   }
}

class B extends A
{
   function example($bool)
   {
      echo "Alice";
      parent::example($bool);
      echo "Charlie";
   }
}

if i call

$x = new B;
$x->example(false);

i´ll get "AliceBobCharlie" as expected

and what i want is that if i pass true only "AliceBob" will appear. say: i want to terminate the execution of $x->example in the parent-method

i hope you got me. how to do this?

thanks for your help


Solution

  • Well...either you return a boolean from A::example() and check it in B::example() to decide if you should continue.

    If you really want to have an interrupt, you can throw an exception. Using exceptions to handle control-flow is bad design however.