Radical_Activity
Radical_Activity

Reputation: 2738

How to exit from a specific function in PHP?

I have multiple nested methods inside a PHP class. What I want to do is, based on certain circumstances, I want to exit from NOT JUST the current method, but 2 above it, then the leftover code should continue running. Now the issue with die(), exit() is that they end the full script and I don't want that. I simply want to go a few methods up and continue the script.

Of course, there's the old school method of returning a value in each method and check if it's false for example. But that way I'll have to write tons of additional code if I have like 50 nested methods. Here's what I have right now - it's a very basic usage here, I'm using it in a lot more complicated scenarios (using PHP 7.2.4):

class Sites
{
    public function __construct()
    {
        $this->fn1();
    }

    public function fn1()
    {   
        $fn2 = $this->fn2();

        echo 'I want this to be displayed no matter what!';
    }

    public function fn2()
    {       
        $fn3 = $this->fn3();

        if ($fn3)
        {
            return true;
        }
    }

    public function fn3()
    {       
        $fn4 = $this->fn4();

        if ($fn4)
        {
            return true;
        }
    }

    public function fn4()
    {       
        $random = rand(1, 100);

        if ($random > 50)
        {
            return true;
        }
        else
        {
            // I want to exit/break the scirpt to continue running after
            // the $fn2 = $this->fn2() call in the $this->fn1() function.
            exit();

            echo "This shouldn't be displayed.";
        }
    }
}

Just as mentioned in the code comments, I want to break the script - if the random number is below 50 and go back to fn1() but continue executing the echo function there.

Is this possible somehow? Please let me know if you need more information and I'll provide.

Upvotes: 2

Views: 11730

Answers (2)

Rain
Rain

Reputation: 3926

How about regular function call with a flag?

class Sites
{
    protected $flag  = false; 
    public function __construct()
    {
        $this->fn1();
    }


    public function fn1()
    {

        if ($this->flag) {
            $this->flag = true;

        } else {
            echo 'I want this to be displayed no matter what!';
            $fn2 = $this->fn2();
        }

    }

    public function fn2()
    {       
        $fn3 = $this->fn3();

        if ($fn3)
        {
            return true;
        }
    }

    public function fn3()
    {       
        $fn4 = $this->fn4();

        if ($fn4)
        {

            return true;
        }
    }


public function fn4()
{
    $random = rand(1, 100);

    if ($random > 50)
    {
        return true;
    }
    else
    {
        // I want to exit/break the scirpt to continue running after
        // the $fn2 = $this->fn2() call in the $this->fn1() function.
        //exit();
        $this->flag = true;
        $this->fn1();
        exit();

        echo "This shouldn't be displayed.";
    }
}
}

$sites = new Sites;

I hope this helps!

Upvotes: 1

Nigel Ren
Nigel Ren

Reputation: 57121

You can use Exceptions to do this, not particularly elegant, but this should do what your after, replace these methods...

public function fn1()
{
    try {
        $fn2 = $this->fn2();
    }
    catch ( Exception $e )  {
    }

    echo 'I want this to be displayed no matter what!';
}


public function fn4()
{
    $random = rand(1, 100);

    if ($random > 50)
    {
        return true;
    }
    else
    {
        // I want to exit/break the scirpt to continue running after
        // the $fn2 = $this->fn2() call in the $this->fn1() function.
        //exit();
        throw new Exception();

        echo "This shouldn't be displayed.";
    }
}

Upvotes: 5

Related Questions