PHP equivalent of Python's __name__ == “__main__”?

徘徊边缘 提交于 2019-12-02 17:28:44
zerkms
if (!count(debug_backtrace()))
{
    // some usefull stuff
}
if ($argv && $argv[0] && realpath($argv[0]) === __FILE__) {
    // ...
}

works like a charm.

when you run php in command line, the name of php file will pass to program as $argv[0] and __FILE__ magic variable mean current file. So we check the running program is current file logically equals Python's __name__ == "__main__".

You probably want one of the "Magic Constants". Depending on what you are trying to do, __FILE__, __FUNCTION__ or __CLASS__ may give you the information you are after.

They are pretty self explanatory:

  • __FILE__gives you the current file name
  • __FUNCTION__ gives you the name of the current function
  • __CLASS__ gives you the name of the current class.

Check the manual for more details

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!