tell if we're actually in the script which was directly launched

Python:

if __name__ == '__main__':
    main()

PHP:

function get_name(){
    
$_name ='';
    
// CLI or apache
    $_entrypoint = PHP_SAPI == 'CLI' ? basename($argv[0]) : basename($_SERVER['PHP_SELF']) ;
    if ( 
basename(__FILE__) == $_entrypoint ) {
        
$_name = '__main__';
    }
    return 
$_name;
}
define('__name__', get_name() );


if ( 
__name__ == '__main__' ) {
    
// run the main or tests
    main();
}

I'm using this trick with lib files, called by cli, I run the PHPUnit separeted test files:

// if colled directly, run the tests:
if (basename($argv[0]) == basename(__FILE__)) {
    include 
substr(__FILE__, 0, -4) . 'Test.php';
}
// unix-style command line filter
$fp = fopen("php://stdin", "r") or die("can't read stdin");
while (!
feof($fp)) {
    
$line = fgets($fp);
    
// convert uppercase letters at the beginning of paragraphs to lowercase
    $line = preg_replace_callback('|<p>\s*\w|', function ($matches) {
            return 
strtolower($matches[0]);
    }, 
$line );
    echo 
$line;
}
fclose($fp);

CLI

class cli{
    function 
prompt($promptStr,$defaultVal=false){

        if(
$defaultVal) {
            
// If a default set
            echo $promptStr. "[". $defaultVal. "] : ";
            
// print prompt and default
        } else {
            
// No default set
            // print prompt only
            echo $promptStr. ": ";
        }

        
$name = chop(fgets(STDIN)); // Read input. Remove CR
        if(empty($name)) {          // No value. Enter was pressed
            return $defaultVal;     // return default
        } else {                    // Value entered
            return $name;           // return value
        }
    }
}
/*
    esegue i metodi invocati a commandline se si chiamano action_*
*/
class cli_controller {
    private function 
action_test(){    }

    private function 
action_usage()
    {
        echo 
" usage: \n";
        foreach( 
get_class_methods($this) as $m){
            if( 
false !== strpos($m, 'action_') ){
                
$m = str_replace('action_', '', $m);
                echo 
"$m \n";
            }
        }
    }
    public function 
run(){
        
$this->db = ;
        if (!isset(
$argv[1])) {
            
$argv[1] = 'usage';
        }

        
$f = 'action_' . $argv[1];
        if( 
method_exists($this, $f) ){
            
$this->$f();
        }
    }
}

// notifica l'amministratore di errori nella procedura di importazione
public static function registerMonitoringMailHook(){

    
register_shutdown_function(function () {
            
$errfile = "unknown file";
            
$errstr  = "shutdown";
            
$errno   = E_CORE_ERROR;
            
$errline = 0;

            
$error = error_get_last();
            
$trace = print_r( debug_backtrace( false ), true );
            
$server_name = `hostname -f`;

            if( 
$error !== NULL) {
                
$errno   = $error["type"];
                
$errfile = $error["file"];
                
$errline = $error["line"];
                
$errstr  = $error["message"];


                
$content = "";
                
$content .= "Error : $errstr ";
                
$content .= "Errno : $errno  ";
                
$content .= "File  : $errfile";
                
$content .= "Line  : $errline";
                
$content .= "Trace : $trace  ";


                
$mail_res = mail($to='test@gmail.com', $subject="import error $server_name", $content);

            }
    });
}