Skip to content

Instantly share code, notes, and snippets.

@saroarhossain57
Last active September 27, 2016 18:56
Show Gist options
  • Save saroarhossain57/84c76477fed3c4aadf3bcc0ae3f499dd to your computer and use it in GitHub Desktop.
Save saroarhossain57/84c76477fed3c4aadf3bcc0ae3f499dd to your computer and use it in GitHub Desktop.
there are eight magical constant that changes depanding on where they are used. here are all magic constant is described
<?php
/*there are eight magical constant that changes depanding on where they are used
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
__FUNCTION__ The function name.
__CLASS__ The class name. The class name includes the namespace it was declared in (e.g. Foo\Bar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
__TRAIT__ The trait name. The trait name includes the namespace it was declared in (e.g. Foo\Bar).
__METHOD__ The class method name.
__NAMESPACE__ The name of the current namespace.
*/
class trick{
public function doit(){
echo __FUNCTION__;
}
public function doitagain(){
echo __METHOD__;
}
}
$obj1 = new trick();
$obj1->doit();
echo '<br>';
$obj1->doitagain();
echo '<br>';
echo '<br>';
function testFunction(){
echo '<br>'.__FUNCTION__;
echo '<br>'.__FILE__;
echo '<br>'.__DIR__;
echo '<br>'.dirname(__FILE__);
}
testFunction();
echo '<br>';
echo '<br>';
//the __CLASS__ magic constant nicely complements the get_class() function
class baseClass{
function say_a(){
echo " 'a' - Said the ". __CLASS__ . "<br>";
}
function say_b(){
echo " 'b' - Said the ". get_class($this) . "<br>";
}
}
class subClass extends baseClass{
function say_a(){
parent::say_a();
echo " 'a' - Said the ". __CLASS__ . "<br>";
}
function say_b(){
parent::say_b();
echo " 'b' - Said the ". get_class($this) . "<br>";
}
}
$obj = new subClass();
$obj->say_a();
$obj->say_b();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment