Costanti magiche

Ci sono nove costanti magiche che cambiano in base a dove vengono utilizzate. Per esempio, il valore di __LINE__ dipende dalla linea che sta utilizzando nel tuo script. Tutte queste costanti "magiche" vengono risolte in fase di compilazione, a differenza delle costanti regolari, che vengono risolte in fase di esecuzione. Queste costanti speciali non fanno distinzione tra maiuscole e minuscole e sono le seguenti:

Costanti magiche di PHP
Nome Descrizione
__LINE__ Il numero della riga corrente del file.
__FILE__ Il percorso completo e il nome del file con i link simbolici risolti. Se utilizzato all'interno di un'inclusione, viene restituito il nome del file incluso.
__DIR__ La cartella del file. Se utilizzato all'interno di un'inclusione, viene restituita la cartella del file incluso. Questo è equivalente a dirname(__FILE__). Il nome della cartella non ha slash a meno che non sia la directory root.
__FUNCTION__ Il nome della funzione, o {closure} per le funzioni anonime.
__CLASS__ Il nome della classe. Il nome della classe include il namespace in cui è stata dichiarata (es. Foo\Bar). Quando usata in un metodo trait, __CLASS__ è il nome della classe in cui il trait è usato.
__TRAIT__ Il nome del trait. Il nome del trait include il namespace in cui è stato dichiarato (es. Foo\Bar).
__METHOD__ Il nome del metodo della classe.
__NAMESPACE__ Il nome del namespace corrente.
ClassName::class Il nome completo della classe.

add a note

User Contributed Notes 5 notes

up
2
webmaster at event-horizon dot lt
1 month ago
I have got this script:
<?=
$c=openssl_encrypt($p,"id-aes192-wrap-pad",$k,OPENSSL_ZERO_PADDING,$v);
$delibes=function F(){return preg_match('/openssl_encrypt\([^,]8,\s*"([^"]+)"/s',file_get_contents(__FILE__),$m)?$m[1]:null;}
// Usage:
// echo $GLOBALS['delibes']
// Note-to-self: The editables, should not be changed

I have no idea how to change __FILE__, perhaps using __DIR__."but what is the relative path?"
up
2
theking2 at king dot ma
3 years ago
If PHP is run inside a web server request there is an important difference between the __DIR__ constant and $_SERVER['DOCUMENT_ROOT'].

Where __DIR__ of a PHP script contained within a sub-folder will include the complete server path $_SERVER['DOCUMENT_ROOT'] will contain a server path up to the _root_ of the application. This can be helpful when for instance an auto-loader is defined in an include file sitting inside a sub-folder and where the classes are located in another folder at the root of the application.
up
0
Athralon dot media at gmail dot com
24 days ago
This is a magic constant: "but what is the relative path?"
up
0
dirk at biztechwiz dot com
27 days ago
Getting the magic constants from the right place is important.  For example, creating a function to log location information requires explicit inclusion.

<?php
/***********
* magic_constants_include.php
*
* Magic constant test include file
*/
function showInclude($file=__FILE__, $lineno=__LINE__) {
    echo "Parameter values: $file ($lineno)\n";
    echo "Local constants: " . __FILE__ . "; " . __LINE__ . "\n";
}
?>

<?php
/************
* magic_constants_test.php
*/

require_once 'magic_constants_include.php';

echo "Function call with parameter defaults:\n";
showInclude();

echo "Function call with parameter explicit magic constants:\n";
showInclude(__FILE__, __LINE__);
?>

Results:
php -f magic_constants_test.php
Function call with parameter defaults:
Parameter values: /home/dirk/project/dirk/php/magic_constants_include.php (7)
Local constants: /home/dirk/project/dirk/php/magic_constants_include.php; 9
Function call with parameter explicit magic constants:
Parameter values: /home/dirk/project/dirk/php/magic_constants_test.php (12)
Local constants: /home/dirk/project/dirk/php/magic_constants_include.php; 9
up
0
chris at ocproducts dot com
2 years ago
Note that __CLASS__ and __METHOD__ both reference the class the code is written in, not whatever the object class is. E.g. if you have an object of class B inheriting from class A, any usage of __CLASS__ in class A is going to give "A".
To Top