How to return false from a PHP script from inside a class/function? -


how can make the main php script return false inside class or function?

why: because of built-in webserver:

if php file given on command line when web server started treated "router" script. script run @ start of each http request. if script returns false, requested resource returned as-is.

from documentation php built-in webserver

in other words, return false in router script built-in webserver can serve static files. example documentation:

if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_server["request_uri"])) {     return false;    // serve requested resource as-is. } else {      echo "<p>welcome</p>"; } 

the thing i'm trying add behavior web framework: don't want write index.php. i'd rather encapsulate logic class (middleware) halt script's execution if php_sapi_name() == 'cli-server' , static asset asked.

however, don't know how can make whole php script return false class or function, since return false return current method/function/file , not main file.

is there way achieve same behavior exit() example? realize don't know return false in main file means (is specific exit code?).

you should have router invoke class method, , then, if method returns false, return false router file.

of course can turn headache. there 2 methods achieve want achieve.

there faster way though, can abuse exceptions , create specialized exception case:

staticfileexception.php

<?php class staticfileexception extends exception {} 

router.php

<?php try {     $c = new controller();     return $c->handlerequest(); } catch (staticfileexception $e) {     return false; } 

once have kind of code in place, throw new staticfileexception , you're done.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -