Php magic variables and magic function examples-php Tutorial

Source: Internet
Author: User
Tags function examples
Php magic variables and magic function examples

  1. Echo 'This is LINE'. _ LINE ';
  2. ?>

Output result: This is line 2.

The complete path and FILE name of The _ FILE. If it is used in a file to be included, the file name to be included is returned. Starting from PHP 4.0.2, __file _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution ), earlier versions sometimes contain a relative path.

Example:

  1. Echo 'The FILE is in '. _ FILE __.'"';
  2. ?>

The output result is: the file is located in "E: \ wamp \ www \ test \ index. php"

The directory where the _ DIR _ file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0)

Example:

  1. Echo 'The file is located in '. _ DIR __.'"';
  2. ?>

The output result is: the file is located in "E: \ wamp \ www \ test"

_ FUNCTION name (new in PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters.

Example:

  1. Function test (){
  2. Echo 'function name: '. _ FUNCTION __;
  3. }
  4. Test ();
  5. ?>

The output result is: function name: test

_ CLASS name (new in PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, the value is always lowercase letters. The class name includes the declared zone (for example, Foo \ Bar ). Note that _ CLASS _ from PHP 5.4 also applies to trait. When used in the trait method, __class _ is the name of the CLASS that calls the trait method.

Example:

  1. Class test {
  2. Function _ print (){
  3. Echo 'CLASS name:'. _ CLASS __."
    ";
  4. Echo 'function name: '. _ FUNCTION __;
  5. }
  6. }
  7. $ T = new test ();
  8. $ T-> _ print ();
  9. ?>

The output result is: class name: test function name: _ print

_ TRAIT _ Trait name (new in PHP 5.4.0 ). Since PHP 5.4.0, PHP has implemented a method for code reuse, called traits. The Trait name includes the declared region (for example, Foo \ Bar ). The member inherited from the base class is overwritten by the MyHelloWorld method in the inserted SayWorld Trait. Its behavior is the same as that defined in the MyHelloWorld class. The priority is that the methods in the current class will overwrite the trait method, and the trait method overwrites the methods in the base class.

Example:

  1. Class Base {
  2. Public function sayHello (){
  3. Echo 'hello ';
  4. }
  5. }
  6. {
  7. Public function sayHello (){
  8. Parent: sayHello ();
  9. Echo 'World! ';
  10. }
  11. }
  12. Class MyHelloWorld extends Base {
  13. }
  14. $ O = new MyHelloWorld ();
  15. $ O-> sayHello ();
  16. ?>

Output: Hello World!

_ METHOD _ class METHOD name (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).

Example:

  1. Function test (){
  2. Echo 'function name: '. _ METHOD __;
  3. }
  4. Test ();
  5. ?>

The output result is: function name: test

_ NAMESPACE _ name of the current NAMESPACE (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0 ).

Example:

  1. Namespace MyProject;
  2. Echo 'namespace: "', _ NAMESPACE __,'" '; // output "MyProject"
  3. ?>

Output: namespace: "MyProject"

Magic functions_ Construct () is called when an object is instantiated. When _ construct and a function with the same class name exist at the same time, __construct is called, and the other is not called. _ Destruct () is called when an object or object operation is terminated. The _ call () object calls a method. if a method exists, it is called directly. if the method does not exist, the _ call function is called. When _ get () reads an object's attribute, if the attribute exists, the property value is directly returned. if it does not exist, the _ get function is called. When _ set () is used to set an object's property, if the property exists, the value is assigned directly. if the property does not exist, the _ set function is called. _ ToString () is called when an object is printed. For example, echo $ obj; or print $ obj ;__ clone () is called to clone an object. For example, $ t = new Test (); $ t1 = clone $ t ;__ sleep () was called before serialize. If the object is large and you want to delete something and serialize it, consider this function. _ Wakeup () unserialize is called to initialize the object. _ Isset () is called to check whether an object's attribute exists. For example, isset ($ c-> name ). _ Unset () unset the attribute of an object is called. For example, unset ($ c-> name ). _ Set_state () is called when var_export is called. Use the return value of _ set_state as the return value of var_export. _ Autoload () is called if the corresponding class does not exist when instantiating an object.

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.