PHP clone revisits object-oriented programming

不言
Release: 2023-03-23 10:28:01
Original
968 people have browsed it

The content introduced in this article is PHP clone revisiting object-oriented programming. Now I share it with everyone. Friends in need can refer to it

php object copy
clone function, copy a new object , all properties need to be kept the same as the original window, but it must be a new object. (If it is not a new object, then changes in one window will affect the other window, or object A holds a reference to object B, and when you copy object A, the object you want to use is no longer object B but object A copy of B. At this time, you need to get a copy of object A.)

PHP deep copy and shallow copy
Starting from PHP5, the new operator automatically returns a reference. An object uses an identifier to access the actual object content.
Deep copy refers to copying the variables of the referenced object only to the new object that has been copied, rather than the original referenced object.
PHP can implement deep copy in two ways, 1️⃣ clone 2️⃣ By serializing the object, first serialize the object and then deserialize it.

ResumeA));

__clone method can have no parameters, it Automatically contains

that two pointers
this refers to the properties of the current object, and that refers to the properties of the old object

Classes and Objects
Class: A collection of objects with the same properties and services.
Object: The result of class instantiation is the object

Construction method:
Only one constructor can be declared in a class, but the constructor will only be called once every time an object is created. method, this method cannot be called actively, so it is usually used to perform some useful initialization tasks. For example, the corresponding properties are assigned initial values ​​when the object is created.

Destructor
Perform some current operations or complete some functions before destroying a class
The destructor will be deleted when all references to an object are deleted or when the object is explicitly destroyed When the destructor is executed, that is, the destructor is called before the object is destroyed in the memory.

The object is actually placed in the memory in the form of a stack, so when the destructor is finally called, it follows the last-in-first-out principle.

Three major characteristics of object-oriented programming:
Encapsulation Inheritance Polymorphism
Encapsulation: combines all properties of the object and all services to form an indivisible independent unit (object), information Hiding, that is, hiding the internal details of the object as much as possible
Inheritance Single inheritance (PHP JAVA, a derived class in C can be derived from multiple base classes) The new data type defined not only has newly defined members, but also Have old members. We call existing classes used to derive new classes as base classes, also known as parent classes and super classes. A new class derived from an existing class is called a derived class, also called a subclass.

PHP overloading refers to the subclass overriding the existing methods of the parent class

final defines attribute members and cannot be overridden by subclasses

static and const
static Description Member properties and member methods are static.
static members can restrict external access because static members belong to the class and do not belong to instances of any objects. They are space allocated when the class is first loaded. , other classes are inaccessible. Only sharing the strength of the class can protect the class to a certain extent.

static Static members cannot be accessed in static methods. Use const to achieve

The access method of member properties of const modifier is similar to the access method of static modified member. It also uses the class name. In the method Self is used inside, but you don’t need to use the $ symbol, and you can’t use objects to access

Serialize the object
There are two situations where we must serialize the object. The first situation is to serialize an object Objects must be serialized when transmitting them over the network. The second case is when serializing objects is written to files or databases.

The parameter of the serialize() function is the object name, and the return value is a string.

When serializing an object, the __sellp —– (some member attributes can be hidden at this time) method will be automatically executed. The deserialization is to execute __wakup() — (can assign values ​​at this time) )method.

__sleep() function does not accept any parameters, but returns an array containing the attributes that need to be serialized. Attributes that are not included will be ignored during serialization. If there is no __sleep() method, PHP will save all attributes.

php object copy
Clone function, copy a new object, need to keep all properties the same as the original window, but it must be a new object. (If it is not a new object, then changes in one window will affect the other window, or object A holds a reference to object B, and when you copy object A, the object you want to use is no longer object B but object A copy of B. At this time, you need to get a copy of object A.)

PHP deep copy and shallow copy
Starting with PHP5, the new operator automatically returns a reference. An object uses an identifier to access the actual object content.
Deep copy refers to copying the variables of the referenced object only to the new object that has been copied, rather than the original referenced object.
PHP can implement deep copy in two ways, 1️⃣ clone 2️⃣ By serializing the object, first serialize the object and then deserialize it.

ResumeA));

__clone method can have no parameters, it Automatically contains

that two pointers
this refers to the properties of the current object, and that refers to the properties of the old object

Classes and Objects
Class: A collection of objects with the same properties and services.
Object: The result of class instantiation is the object

Construction method:
Only one constructor can be declared in a class, but the constructor will only be called once every time an object is created. method, this method cannot be called actively, so it is usually used to perform some useful initialization tasks. For example, the corresponding properties are assigned initial values ​​when the object is created.

Destructor
Perform some current operations or complete some functions before destroying a class
The destructor will be deleted when all references to an object are deleted or when the object is explicitly destroyed When the destructor is executed, that is, the destructor is called before the object is destroyed in the memory.

The object is actually placed in the memory in the form of a stack, so when the destructor is finally called, it follows the last-in-first-out principle.

Three major characteristics of object-oriented programming:
Encapsulation Inheritance Polymorphism
Encapsulation: combines all properties of the object and all services to form an indivisible independent unit (object), information Hiding, that is, hiding the internal details of the object as much as possible
Inheritance Single inheritance (PHP JAVA, a derived class in C can be derived from multiple base classes) The new data type defined not only has newly defined members, but also Have old members. We call existing classes used to derive new classes as base classes, also known as parent classes and super classes. A new class derived from an existing class is called a derived class, also called a subclass.

PHP overloading refers to the subclass overriding the existing methods of the parent class

final defines attribute members and cannot be overridden by subclasses

static and const
static Description Member properties and member methods are static.
static members can restrict external access because static members belong to the class and do not belong to instances of any objects. They are space allocated when the class is first loaded. , other classes are inaccessible. Only sharing the strength of the class can protect the class to a certain extent.

static Static members cannot be accessed in static methods. Use const to achieve

The access method of member properties of const modifier is similar to the access method of static modified member. It also uses the class name. In the method Self is used inside, but you don’t need to use the $ symbol, and you can’t use objects to access

Serialize the object
There are two situations where we must serialize the object. The first situation is to serialize an object Objects must be serialized when transmitting them over the network. The second case is when serializing objects is written to files or databases.

The parameter of the serialize() function is the object name, and the return value is a string.

When serializing an object, the __sellp —– (some member attributes can be hidden at this time) method will be automatically executed. The deserialization is to execute __wakup() — (can assign values ​​at this time) )method.

__sleep() function does not accept any parameters, but returns an array containing the attributes that need to be serialized. Attributes that are not included will be ignored during serialization. If there is no __sleep() method, PHP will save all attributes.

Related recommendations:

Detailed explanation of references and clone in PHP


The above is the detailed content of PHP clone revisits object-oriented programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template