PHP Magic Variable Summary

Source: Internet
Author: User

Explanations in the PHP manual

__FUNCTION__ returns only the name of the function returns only the names of the functions

__METHOD__ returns the name of the class alongwith the "the" function returns the names of the functions, if the method name of the class is taken out, and __method__ can not only remove the method name, You can also remove the class name

1 classTrick2 {3       functiondoit ()4       {5                 Echo __function__;6       }7       functionDoitagain ()8       {9                 Echo __method__;Ten       } one } a $obj=NewTrick (); - $obj-doit (); - //output'll be ----doit outputs doit the $obj-Doitagain (); - //output'll be -----trick::d oitagain output trick::d oitagain

Name of the __class__ class

Get_class () returns the class name of the object , string get_class ([ object $obj ]), which returns obj the name of the class to which the object instance Belongs. objreturns if it is not an object FALSE

The __class__ magic constant nicely complements the Get_class () Function. The magic constant __class__ nicely complements the Get_class () function

Sometimes need to know both: sometimes you will know the following two
-name of the inherited class inherits class names
-name of the class actually executed actually executed

Here's A example that shows the possible solution: the following is an example

1<?PHP2 3 classBase_class4 {5     functionsay_a ()6     {7         Echo"' a '-said the".__class__. "<br/>";8     }9 Ten     functionSay_b () one     { a         Echo"' b '-said the".Get_class($this) . "<br/>"; -     } -  the } -  - classDerived_classextendsBase_class - { +     functionsay_a () -     { +parent::say_a (); a         Echo"' a '-said the".__class__. "<br/>"; at     } -  -     functionSay_b () -     { -parent::Say_b (); -         Echo"' b '-said the".Get_class($this) . "<br/>"; in     } - } to  + $obj _b=NewDerived_class (); -  the $obj _b-say_a (); * Echo"<br/>"; $ $obj _b-Say_b ();Panax Notoginseng  -?>

Content of the Output:
' A '-said the Base_class
' A '-said the Derived_class

' B '-said the Derived_class
' B '-said the Derived_class

The directory where the __dir__ file Resides. If used in the included file, returns the directory where the included files are Located. It is equivalent to dirname (__file__) (new in PHP 5.3.0)

The full path and file name of the __file__ File. If used in included files, returns the included file name

If you want to use in undefined __dir__, you can use the following methods:

<?PHP//the First Method:if(!defined(' __dir__ ')) {$iPos=Strrpos(__file__, “/");Define("__dir__",substr(__file__, 0,$iPos) . “/");}//the Second Method:functionAbspath ($file){  return Realpath(dirname($file));} Abspath (__file__);?>

__NAMESPACE__ the name of the current namespace (case sensitive). This constant is defined at compile time (PHP 5.3.0 new)

__static__ when you call the Class's static method, it returns the class name, which is Case-sensitive. If called in inheritance, the inherited class name can be returned regardless of whether there is a definition in the Inheritance.

The current line number in the __line__ file.

__trait__ TRAIT's name (PHP 5.4.0 new addition). From PHP 5.4 This constant returns the name of the trait when it is defined (case-sensitive). The Trait name includes its declared function area (for example, foo\bar).

PHP Magic Variable Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.