Last active
August 29, 2015 14:08
-
-
Save dcb9/db25cff0ee916d720c9e to your computer and use it in GitHub Desktop.
用php模拟python的 __name__ == '__main__' 功能,这样可以做一些测试,只有直接运行这个php的时候才执行,如果这个php是被包含的则不执行,并且还要是在终端下。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ismain(){ | |
$debug = debug_backtrace(); | |
if(PHP_SAPI === 'cli' && count($debug)==1 && $debug[0]["function"]==__FUNCTION__){ | |
return true; | |
} | |
return false; | |
} | |
if ( ismain()){ | |
// do something... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment