php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33894 __MAIN_FILE__ constant
Submitted: 2005-07-27 23:27 UTC Modified: 2010-12-22 03:46 UTC
From: marcos dot neves at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.1.0b3 OS: any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: marcos dot neves at gmail dot com
New email:
PHP Version: OS:

 

 [2005-07-27 23:27 UTC] marcos dot neves at gmail dot com
Description:
------------
Today we have __FILE__ constant, that has the string fileName of the current scrit. Would be nice a constant __MAIN_FILE__, wich has the file name of the first requested PHP file.
So would be possible to do things like this, without use of external library, and is interesting to everyone:

Reproduce code:
---------------
<?

// class File

class Foo {}

if(__MAIN_FILE__ == __FILE__) {
// run some tests and usabe examples with Foo
}

?>

or used to include relative paths to an application, without define constants

<?

// included script

require_once dirname(__MAIN_FILE__) . DIRECTORY_SEPARATOR . "path/to/include/file.php";

?>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-08-01 13:28 UTC] derick@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

We've PHP_SELF for this.
 [2005-08-01 14:08 UTC] marcos dot neves at gmail dot com
$_SERVER['PHP_SELF'] hasn't the same value using php with apache and php cli.
Run <? echo $_SERVER['PHP_SELF'] ?> in a browser and as command line, you'll see the difference.

At browser works fine do the follow:

if(__FILE__ == $_SERVER['PHP_SELF']) {
}

but does not work with cli.

Tested with PHP 5.1.0b3 on windows XP
 [2010-12-22 03:46 UTC] johannes@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-12-22 03:46 UTC] johannes@php.net
PHP_SELF is the best we can offer. Mind that the "main" file is not always clear - think aboutauto_prepend_file/auto_append_file etc.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 15:01:33 2025 UTC