I'm looking for something like break for loops.

Here's some example code (using Symfony's lime) where stop() would not let the class continue and I_DONT_WANT_THIS_TO_RUN() would not be executed.

$browser->isStatusCode(200)
  ->isRequestParameter('module', 'home')
  ->isRequestParameter('action', 'index')
  ->click('Register')
  ->stop()
  ->I_DONT_WANT_THIS_TO_RUN();
$browser->thenThisRunsOkay();

Calling $this->__deconstruct(); from within stop() doesn't seem to do the trick. Is there a function I can call within stop() that would make that happen?

Accepted Answer

Just return another class which will return $this for every method called.

Example:

class NoMethods {
    public function __call($name, $args)
    {
        echo __METHOD__ . " called $name with " . count($args) . " arguments.\n";
        return $this;
    }
}

class Browser {
    public function runThis()
    {
        echo __METHOD__ . "\n";
        return $this;
    }

    public function stop()
    {
        echo __METHOD__ . "\n";
        return new NoMethods();
    }

    public function dontRunThis()
    {
        echo __METHOD__ . "\n";
        return $this;
    }
}

$browser = new Browser();
echo "with stop\n";
$browser->runThis()->stop()->dontRunThis()->dunno('hey');
echo "without stop\n";
$browser->runThis()->dontRunThis();
echo "the end\n";

Will result in:

with stop
Browser::runThis
Browser::stop
NoMethods::__call called dontRunThis with 0 arguments.
NoMethods::__call called dunno with 1 arguments.
without stop
Browser::runThis
Browser::dontRunThis
the end
Written by OIS
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki