Usage of magic methods in PHP
/** PHP treats all class Methods Starting with _ (two underscores) as magic methods. Therefore, when you define your own class methods, do not use _ as the prefix. ** // _ Tostring, _ set, _ GET _ isset (), and _ unset (
Talking about the magic methods in Object-Oriented OOP in PHP, object-oriented oop
1. What is a magic method:
PHP provides us with a series of functions starting with _. These functions are automatically called at the right time without manual calls.
Yii framework uses magic methods to implement cross-file call function examples. yii framework
This example describes how to use the Yii framework to implement cross-file calls using magic methods. We will share this with you for your reference.
Next I will introduce all the magic methods in PHP. In fact, most of the following methods are familiar to a qualified PHP programmer. I will make a summary here, learn the records myself, and give an introduction to the students who are not
Php's method of starting with two underscores is called a magic method, which plays a pivotal role in PHP. Magic methods include: __construct (), class constructor __destruct (), Class destructor __call (), call to an inaccessible method in an
PHP Magic methods and Magic variablesMagic Method:PHP treats all class methods starting with __ (two underscores) as a magic method;
__construct (), construction method;
__destruct (), destructor;
__get (String $name) is called when
__construct: constructor, once the object is created, it is automatically called__call: Automatically triggers when undefined methods are called__set: Automatically triggers when a value is set to a property outside the class that is not accessible__
Common magic Methods are: __tostring () __call () __autoload () __ Clone () __get () __set () __isset () __unset ()1.__tostring () When you define an output object reference, the information that is often used to print some objects must have a
PHP is called the Magic method by using the two-underlined method, which plays an important role in PHP. Magic methods include:
__construct (), constructor for class
__destruct (), Destructor of class
__call () Called when an inaccessible
Let's take a look at the explanations of the PHP official documentation:__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
PHP: Share 5 recently used magic methods 1. _ contruct ()
Constructor. The difference between this and a common constructor is that you do not need to define a function with the same name as the class name every time as a constructor.
2. _
Since python2.2, the classes and types have been unified,The practice is to convert int (), float (), str (), list (), tuple () to factory functionFactory functions: Is an object that, when you call them, is actually creating a corresponding
Inverse operation:Called when the left operand does not support a response operationsuch as: Object (A+B), if the A object has a __add__ method, the __radd__ of the B object is not calledThe __radd__ method of B is called only if the __add__ method
definition: PHP retains all class methods starting with (two underscores) as a magic method. Therefore, in addition to the above magic method when defining a class method, it is not recommended to assume a prefix.
role : Using the Schema method
Php object-oriented magic methods are described in Chinese. 1. the _ construct () instantiated object is automatically called. When _ construct and a function with the class name exist at the same time, _ construct is called, and the other one does
1,__get, __setThese two methods are designed for properties that are not declared in the class and their parent class:__get ($property) accesses this method when an undefined property is called;__set ($property, $value) is called when assigning a
Reflection operationsWhat is the reflection operator, in fact, is reversed two objects, the following first look at the implementation of a common runner:classFoo (object):def __init__(self, x): self.x=xdef __add__(self, Other):return 'foo:%s
1. _ get, _ set
These two methods are designed for attributes that are not declared in the class and their parent class.
_ Get ($ property) access this method when an undefined property is called
_ Set ($ property, $ value) is called when an
For PHP loading operations and other two magic methods, load magic
Load class// Include ("./Ren. class. php ");// Include "./Ren. class. php ";Include_once ("./Ren. class. php ");Include_once ("./Ren. class. php ");$ F = new Ren ();$ F-> test
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.