Note a small inconsistency when using __CLASS__ and __METHOD__ in traits (stand php 7.0.4): While __CLASS__ is working as advertized and returns dynamically the name of the class the trait is being used in, __METHOD__ will actually prepend the trait name instead of the class name!

Just learned an interesting tidbit regarding __FILE__ and the newer __DIR__ with respect to code run from a network share: the constants will return the *share* path when executed from the context of the share.

A lot of notes here concern defining the __DIR__ magic constant for PHP versions not supporting the feature. Of course you can define this magic constant for PHP versions not yet having this constant, but it will defeat its purpose as soon as you are using the constant in an included file, which may be in a different directory then the file defining the __DIR__ constant. As such, the constant has lost its *magic*, and would be rather useless unless you assure yourself to have all of your includes in the same directory.

Concluding: eye catchup at gmail dot com's note regarding whether you can or cannot define magic constants is valid, but stating that defining __DIR__ is not useless, is not!

What eyecatchup and I posted are good one-liners, but they are fundamentally flawed. Magic constants cannot be defined in a backward-compatible manner in PHP code because the value of a magic constant is defined at run-time based on the current context.

__DIR__ is defined relative to the current file. If you define it in one file using the method that I or eyecatchup posted, then the value is dependant upon the location of the file where it is defined.

As pointed out by david at thegallagher dot net[1], you can NOT use the defined() function to check if a *magic* constant is defined. Often seen, but will not work:<?php if (!defined('__MAGIC_CONSTANT__')) {// FAIL! even if __MAGIC_CONSTANT__ is defined, // defined('__MAGIC_CONSTANT__') will ALWAYS return (bool)false.}?>

Now, raat1979 at gmail dot com[2] pointed out a solution to check if a magic constant is defined or not (which actually works reliable). Thanks to dynamic typecasting in PHP, if a constant lookup fails PHP interprets the given constant name as string (note that a notice is thrown nonetheless. thus, use "@" to suppress it).<?php var_dump(@UNDEFINED_CONSTANT_NAME); //prints: string(23) "UNDEFINED_CONSTANT_NAME" ?>

Meaning we can check for all constants - including magic constants (eg __DIR__) - as follows:<?phpif (@__DIR__ == '__DIR__'){// __DIR__ was interpreted as string. thus, (magic) constant __DIR__ is not defined. }?>

However, what is wrong in raat1979 at gmail dot com's note[2] is this comment:> "remember that because they are MAGIC constants defining __DIR__ is completely useless"

In fact, you *can* define magic constants (as long as they haven't been defined before, of course).

Based on all I've read and tested today, here is my code I use to make the `__DIR__` magic constant work with all PHP versions (4.3.1 - 5.5.3):<?php// Ensure that PHP's magic constant __DIR__ is defined - no matter of the PHP version.

You cannot check if a magic constant is defined. This means there is no point in checking if __DIR__ is defined then defining it. `defined('__DIR__')` always returns false. Defining __DIR__ will silently fail in PHP 5.3+. This could cause compatibility issues if your script includes other scripts.

Using __METHOD__ always return class name the function belong to. But in some cases (like to make log output), if class B1, B2, B3 inherit class A and call a method of class A, you may would like to see into log which class among B1, B2, B3 is calling.

This is class A::commond_method_inside_framework A::commond_method_inside_framework This is class B1::commond_method_inside_framework A::commond_method_inside_framework This is class B1::commond_method_inside_caller B1::commond_method_inside_caller