Welcome Guest, Not a member yet? Register   Sign In
How to stop execution of a script midway ?
#1

[eluser]WarDoGG[/eluser]
I had a question about abruptly stopping script execution using die() and also displaying content before 'dying'

Code:
class MyClass extends Controller {

     function doThis()
     {
         $this->mylibrary->checkIfExists();
         // some code
         // some more code
     }

}

And in the mylibrary i were to write

Code:
class Mylibrary
{
   function checkIfExists()
    {
        echo "something something";
        die();
    }
}


nothing shows up on the screen. Anyway i can output the content on the screen and STOP script execution in the library itself without executing the //some code and //somemorecode lines ?
#2

[eluser]bretticus[/eluser]
There are output functions, etc to run before the index.php script is done (which is the only script that ever really runs.) If you call die() within the controller those functions, etc will never be called (hence a blank page.) I think what you want to do is call return instead.

Code:
class Mylibrary
{
   function checkIfExists()
    {
        echo "something something";
        if ( $not_exists )
            return FALSE;

        // other code below...

    }
}
#3

[eluser]WarDoGG[/eluser]
What this is doing is, it is returning the value FALSE to the calling method.

What i want is, the script execution to stop midway in the library itself without returning to the controller.

[quote author="bretticus" date="1269056825"]There are output functions, etc to run before the index.php script is done (which is the only script that ever really runs.) If you call die() within the controller those functions, etc will never be called (hence a blank page.) I think what you want to do is call return instead.

Code:
class Mylibrary
{
   function checkIfExists()
    {
        echo "something something";
        if ( $not_exists )
            return FALSE;

        // other code below...

    }
}
[/quote]
#4

[eluser]bretticus[/eluser]
What you are asking just isn't possible. At some point in your class, you must return somewhere. You can't just kill execution because the rest of the script must continue running outside of your library. You do not have to return FALSE, you can call return all by itself if you prefer, but calling exit() or die() is going to kill the WHOLE script every time.
#5

[eluser]cahva[/eluser]
My 2 cents.. bretticus is right. Theres really something wrong if you have to call die() or exit() function in the middle of library or controller.

Returning false means that you can control the running of your page. Below is the usual way of doing things.
Code:
class MyClass extends Controller {

    function __construct()
    {
        parent::__construct();
    }

    function doThis()
    {
        if (!$this->mylibrary->checkIfExists())
        {
            // some code.. For example load->view with errors etc.
            
            return; // This will stop execution further
        }
        
        // code after this will continue if $this->mylibrary->checkIfExists() returned something
        
        // some code
        // some more code
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB