Examples of functions and usage of common magic methods in PHP, and php magic
Overview
In object-oriented programming, PHP provides a series of magic methods, which provide a lot of convenience for programming. The magic method in PHP usually starts
Methods for generating objects:1, from the class to produce the object. New, open up a memory space through new to the heap area2. Object generation from Object. clone, through the keyword clone, a complete copy of an object, a new piece of memory
Property overloading: When accessing a property that does not exist or has insufficient permissions, it can trigger a series of magic methods called Property overloading__unset (): Automatically triggers when a property that does not exist or has
_class_: Returns the class name of the current class _method_: Returns the method name of the current class method (and displays the class's Invocation, Class Name:: Method Name)_function_: Returns the function name of the current function_file_:
I checked the following manual. For php magic quotes, several common settings are as follows: magic_quotes_gpc, magic_quotes_sybase, and magic_quote_runtime. These functions are in php. from the manual, we can see that these features have been
This article describes how to use PHP magic methods _ GET and _ SET. This article provides detailed comments in the code. For more information, see _ get () -when you read the value of an inaccessible attribute, __get () will be called.
_ Set
addlist (' Dalisng ', 234); Echo $computer 1; *///__call () mask the error generated when invoking the method, and when we call a nonexistent method, the __call () method is called automatically. Independent example, two instances were established,
Php magic methods _ get () and _ set () are introduced. if you need them, refer to the instructions in the official php documentation:
_ Set () is run when writing data to inaccessible properties.
_ Get () is utilized for reading data from
This article mainly introduces the PHP magic method of __call and __callstatic How to use, the need for friends can refer to the next
Core code
Magic Method __call/* $method Get the method name $arg get the parameter collection of the method
Php magic variables and magic function examples
Echo 'This is LINE'. _ LINE ';
?>
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
This article mainly introduces some common php magic methods and provides a brief description of the differences and relationships between them. If you need a small partner, study them. Php requires that all methods starting with two underscores (_)
This article describes how to use PHP magic methods, this article describes how to use magic methods such as _ get, _ set, _ call, _ callStatic, _ toString, and _ invoke. For more information, see
① _ Get/_ set: takes over the attributes of an
The __get () method, which is explained in the official manual, is called when a class property or method that is not defined or is not visible in the current environment is invoked (by adding: or Setting | assignment). these undefined or invisible
1. What is the function of magic quotes?
?The Magic quote design was designed to escape from a database or file and receive parameters from a request, with single quotes, double quotes, backslashes, and null plus a backslash, which works
Call and callStatic methods of PHP magic methods _ call are used to call undefined methods in the class, __callstatic is used to call static methods called in the class. both functions accept two parameters, the first parameter is the function name,
The magic method _ call () ,__ clone () ,__ set () ,__ get () in php (). Magic methods in php _ call () ,__ clone () ,__ set () ,__ get () _ call ($ a, $ v ), when the external call class does not exist or is invisible (using protected, private
PHP Magic function
Class person{private $name; Private $age; Private $sex; Public function __construct ($name, $age, $sex) {$this->name= $name; $this->age= $age; $this->sex= $sex; Public Function __get ($property)
__sleep ():
When using serialize () serialization, it detects if there is a __sleep () Magic method in the class, and some calls __sleep () to return an array containing all the variable names that need to be serialized in the object.
Look first.
Official DocumentsThe explanation
__set () is the run when writing data to inaccessible properties.
__get () is utilized for reading data from inaccessible properties.
How do you translate it in Chinese? Inaccessible:n. Hard to reach,
PHP magic examples
① _ Get/_ set: takes over the attributes of an object.
When accessing a non-existent object property:
Index. phpCopy codeThe Code is as follows:Define ('basedir' ,__ DIR _); // defines the root directory constantInclude BASEDIR. '/
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.