Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: is it possible to end a script like exit() but from inside a php class/object

Is it possible to do this?

$objetc -> runAndFinish();

echo "this should not be echoed";

instead of this?

$objetc -> runAndFinish();

exit();

echo "this should not be echoed";

So runAndFinish(); method would somehow end the script processing. Is it possible?

like image 728
Hernantz Avatar asked Jan 12 '11 16:01

Hernantz


People also ask

How do you end a PHP script?

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.

Which of the following function is used to terminate the script execution in PHP?

exit(): The function terminates the execution of a script.

What does exit 0 do in PHP?

Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.


1 Answers

Put an exit(); inside of your classes runAndFinish(); method

class someClass{
  function runAndFinish(){
     exit();
  }
}

$obj = new someClass();
$obj->runAndFinish();
echo "not gonna print";
like image 184
Geoffrey Wagner Avatar answered Sep 27 '22 22:09

Geoffrey Wagner