Some things, if not often used, are easy to forget, such as magic methods and magic constants. Magicmethods (Magicmethods) PHP calls the methods starting with two underscores _ as magic methods, which play an important role in PHP. Magic methods
Some things are easy to forget if they are not used frequently, such as magic methods and magic constants. This article mainly introduces how to use php magic methods (Magicmethods, interested friends can refer to PHP to refer to the methods
In php, magic methods and keywords are commonly used. keywords include final, static, const, magic method _ call (), _ toString (), and _ clone (), _ autoload () and so on...
In php, magic methods and keywords are commonly used. keywords include
When the tostring () magic method treats an object as a string, it automatically calls this method and returns a certain string in this method, to indicate the result after the object is converted to a string. This magic method is commonly used.
_ FILE __: a PHP magic constant. it returns the complete path and FILE name of the current PHP script, including an absolute path. 1) the dirname (_ FILE _) function returns the path where the script is located and updates the network, such as FILE
_ FILE __: a PHP magic constant. it returns the complete path and FILE name of the current PHP script, including an absolute path 1) dirname (_ FILE __) the function returns the path where the script is located.
_ FILE __: a PHP magic constant. it
The difference between magic variable __method__ and __function__ in PHP, Functionmethod
This article explains the difference between magic variable __method__ and __function__ in PHP, and shares it for everyone's reference. The specific analysis
Magic function1. __construct ()Called when an object is instantiated,When __construct and a function with a class name are present, __construct is called and the other is not called.2. __destruct ()Called when an object is deleted or when an object
__construct (): constructor__destruct (): destructor__call () __call () is called when a non-accessible method is called in the object.When __get () reads the value of an inaccessible property, __get () is called.__set () __set () is called when a
Magic variable
PHP provides a large number of predefined constants to any script it runs.
However, many constants are defined by different extensions, and are only available when the extensions are loaded, either dynamically or at compile time.
In a project, if the magic quotes are open, all backslashes (\), single quotes ('), double quotes ("), and NULL characters are automatically escaped with a backslash, which is exactly the same as the addslashes () function. Here are four related
Copy Code code as follows:
/** PHP treats all class methods that start with __ (two underscores) as magic methods. So when you define your own class method, don't prefix it with __. * */
__tostring, __set, __get__isset (), __unset ()/*The __
__isset () – Automatically executes the __isset () method when the Isset () method is used for attributes in a class or in a non-class property, if there are no or no public properties
__unset ()-method for automatically executing __unset () when a
①__get/__set: Takes over the properties of an object
When you access an object property that does not exist:
index.php
Copy Code code as follows:
Define (' BASEDIR ', __dir__); Defining Root Constants
Include BASEDIR. '
I'll explain all the magic methods in PHP below. In fact, most of the following methods, for a qualified PHP programmer, are very familiar with things. I am here to make a summary, I make a record of learning, but also not familiar with these
What is a magic function?
The functions that begin with __ are named Magic Functions, which are triggered under certain conditions. For example: __set () __get (), etc.
triggered when setting or fetching properties that do not exist.
What are the
__get ()-When reading the value of an inaccessible property, __get () is invoked.
__set ()-__set () is invoked when a value is assigned to an inaccessible property.
Copy Code code as follows:
/** * A clear Understanding __get () __
First look at the explanation of the official PHP documentation:
__set () is run when writing the data to inaccessible properties.
__get () is utilized to reading data from inaccessible properties.
How do you translate it in Chinese?Inaccessible:n.
From the PHP 5 version, the class in PHP can use the Magic method. The rules that start with two underscores (__) are reserved as magic methods, so it is recommended that you do not start with a function name unless you are trying to overload an
PHP provides two magic reference functions MAGIC_QUOTES_GPC and magic_quotes_runtime that we can use to reference data, and if the php.ini is set to ON, it will run into single quotes ' and double quotes ' and backslashes for the data we're
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.