The meaning of a variable that starts with an underscore in PHP

Source: Internet
Author: User
Tags php class

Named rules

    • Add one for private

    • Add two is usually the system default, the system is predefined, that is, the so-called: "Magic Method" and "Magic constant"

The constant that PHP starts and ends with double underlines is "magic constant":

The current line number in the __line__ file.

The full path and file name of the __file__ file.

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__). Unless it is a root directory, the name in the directory does not include the trailing slash

Note: The above is from the "PHP Chinese Manual, language Reference, constant-and magic constant".

From the later version of PHP5, the PHP class will be able to use the Magic method.

PHP rules to start with two underscore (__) methods are preserved as a magic method, it is recommended that the function name is best not to start without __, unless it is to reload the existing magic method.

The Magic Methods in PHP are: __construct, __destruct, __call, __callstatic,__get, __set, __isset, __unset, __sleep, __wakeup, __toString, __set_state, __clone, __autoload

__autoload

The __autoload function, which is called automatically when trying to use a class that has not yet been defined. By calling this function, the scripting engine has the last chance to load the required classes before PHP fails.

If you are defining a global auto-load class, you must register the processing class with the PHP standard library using the Spl_autoload_register () method:

<?php    class loader     {        static function autoload_ Class ($class _name)         {         //look for the right $class_name class, and introduce, no then throw an exception         }     }        /**  *   Setting Object Auto-loading   *   spl_autoload_register - register given function as __autoload ()   Implementation  */    spl_autoload_register (Array (' Loader ',  ' Autoload_class ');         $a  = new test ();//test does not use require on the instantiation, realizes the automatic loading, Many frameworks use this method to automatically load class         ?> 

Note: Exceptions thrown in the __autoload function cannot be caught by a catch statement block and cause a fatal error, so the function itself should be captured.

(Original post: http://blog.csdn.net/zlking02/article/details/6752256)

The meaning of a variable that starts with an underscore in PHP

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.