tags:

views:

204

answers:

3

As per the title, is there PHP equivalent of __name__ == "__main__"?

Is there something that would work for both scripts executed through the command line and through a web request, or would a custom function be needed?

For those unfamiliar with Python, __name__ == "__main__" allows you to define a module file, and also have some things that allow you to run it if it is the entry point. The equivalent structure in PHP would resemble this:

// SomeClass.php
<?php
class SomeClass
{
    function doStuff() {
        echo "wahey!\n";
    }
}

// python, I know.
if (__name__ == "__main__") {
    $sc = new SomeClass;
    $sc->doStuff();
}
?>

// OtherClass.php
<?php
require_once("SomeClass.php");
class OtherClass
{
    public $yep;
}
?>

// command line:
php SomeClass.php     // outputs "wahey!"
php OtherClass.php    // outputs nothing

Note: zerkms' answer is the best, but is not quite right - it should read:

if (!debug_backtrace()) {
    // do useful stuff
}

This is significantly faster than !count(debug_backtrace()), which itself is about twice as fast as my solution involving realpath().

+2  A: 
if (!count(debug_backtrace())
{
    // some usefull stuff
}
zerkms
php_sapi_name() just tells me what the code is being executed in, which may provide a small part of the puzzle but doesn't solve the problem. I added some further information to the question to explain what I'm getting at.
Shabbyrobe
... or debug_backtrace() ;-) look at the comment to the question
zerkms
Ahh, finally, I understand. debug_backtrace() works, and is thousands of times faster than the realpath. Update your answer to remove the reference to php_sapi_name() and update your example to read if (!debug_backtrace()) { /* do stuff */ } (or better) so I can accept.
Shabbyrobe
There's no need to do the count - it's actually only twice as fast as the realpath() call if you do the count around it, as opposed to thousands of times faster if you do the simple truth test.
Shabbyrobe
This is not the place to do optimizations. Your algorithms will work >10x times slower than this count(), but with count code become more obvious.
zerkms
how does it become more obivous? "if (!debug_backtrace())" *clearly* says in code "if there is no backtrace then ...". so, micro-optimisation it may be, but why code something that's arguably no clearer and is slower?
Shabbyrobe
because it become depending on implicit php casting array() -> false.i think that explicit "if count of elements is 0" is more cleaner.so i think the reason of using your case is only "readable from your point of view" but not "optimization issues"
zerkms
I think we'll have to agree to disagree on this.
Shabbyrobe
A: 

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

Brenton Alker
Probably, but how do I go about achieving what I am asking with those?
Shabbyrobe
Looks like you've figured it out in your own answer :)
Brenton Alker
+2  A: 

I think I've got it:

if (realpath($_SERVER['SCRIPT_FILENAME']) == __FILE__) {
    // do stuff
}

This caters for the following situations:

  1. Web request
  2. CLI where PWD is the same as the file's (php file.php)
  3. CLI where PWD is explicitly declared (php /path/to/file.php or php ~/path/to/file.php)
  4. CLI where PWD is relative to PWD (php ../file.php)

Other things I tried were $_SERVER['SCRIPT_FILENAME'] == __FILE__, which only works for cases 1 and 2; $argv[0] == __FILE__ which only works for case 3; and basename($argv[0])==basename(__FILE__), which is fundamentally flawed as it is entirely possible to have two files called "controller.php" in any given program.

Is there a better way to do this that doesn't use the realpath call and still satisfies all four criteria?

Shabbyrobe
This seems about right. Why not use realpath?
Justin Johnson
I dunno, actually. Bogus micro-optimisation?
Shabbyrobe
why so complex things while you can count(debug_backtrace())? ;-))
zerkms
because i worked this out before you posted and before i understood how debug_backtrace() could be used
Shabbyrobe