PHP clone revisit object-oriented programming

Source: Internet
Author: User

This article introduces the content of PHP clone revisit object-oriented programming, now share to everyone, the need for friends can refer to

PHP Object replication
Clone function, copy a new object, you need to keep all properties identical to the original window, but must be a new object. (If not a new object, the change in one window will affect the other window, or object A holds a reference to object B, when you copy object A, the object you want to use is no longer object B but a copy of object B, this time you need to get a copy of object a)

PHP deep copy and shallow copy
PHP5 begins, the new operator automatically returns a reference. An object accesses the true object content through an identifier.
A deep copy is a new object that refers to a variable that only wants to be copied, rather than the original referenced object.
A deep copy of PHP can be done in two ways, 1️⃣clone 2️⃣ the object by serializing it before deserializing it.

RESUMEA));

The __clone method can have no parameters and it automatically contains

That two pointers
This refers to the property of the current object, that is, the property of the old object

Classes and objects
Class: A collection of a set of objects that have the same properties and services.
Object: The result of a class instantiation is an object

Construction Method:
Only one construction method can be declared in a class, but a constructor is called every time the object is created, and the method cannot be invoked actively, so it is often used to perform some useful initialization tasks. For example, an attribute is assigned an initial value when creating an object.

Destructors
Do some of the current operations or perform some functions before destroying a class
Destructors are removed from all references to an object or executed when an object is explicitly destroyed, that is, the object calls the destructor before it is destroyed in memory

The class is actually placed in memory as a stack, so when the destructor is last called, it follows the principle of LIFO

The three main features of object-oriented programming:
Encapsulating inherited polymorphism
Encapsulation: Combine all the attributes of an object with all the services to form an indivisible independent unit (object), information hiding, that is, hiding the inner details of the object as much as possible.
Inherited single inheritance (a derived class in PHP java,c++ can derive from more than one base class) defines a new data type that not only has the newly defined member, but also has an old member. We call the existing classes used to derive new classes as base classes, also known as parent classes, and superclass classes. A new class derived from an existing class is called a derived class, also known as a subclass.

PHP overloading refers to a subclass that overrides a method already in the parent class

Final defines attribute members, not quilt class overrides

Static and const
Static describes the member properties and the member methods are statically,
Static members can restrict external access, because static members belong to the class, are instances that are not part of any object, and are allocated space the first time the class is loaded, and other classes are inaccessible. Only for the strength of the class to share, to a certain extent, the protection of the class.

Static methods cannot be accessed by static members using Const to implement

The Const modifier's member property is accessed in much the same way as a static decorated member, using the name of the class, using self in the method, but not using the $ symbol, or using the object to access

Serialization of objects
There are two cases where we have to serialize objects, the first is to serialize an object when it is being transmitted over the network, and the second is to serialize the object when it is written to a file or database.

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

When serializing an object, the __sellp-– (which can hide some member properties at this time) is executed automatically, and deserialization is the method that performs __wakup ()-(which can be assigned at this time).

The __sleep () function does not accept any arguments, but returns an array that contains the properties that need to be serialized. At the end of the contained property will be ignored at serialization, and if there is no __sleep () method, PHP will save all properties.

PHP Object replication
Clone function, copy a new object, you need to keep all properties identical to the original window, but must be a new object. (If not a new object, the change in one window will affect the other window, or object A holds a reference to object B, when you copy object A, the object you want to use is no longer object B but a copy of object B, this time you need to get a copy of object a)

PHP deep copy and shallow copy
PHP5 begins, the new operator automatically returns a reference. An object accesses the true object content through an identifier.
A deep copy is a new object that refers to a variable that only wants to be copied, rather than the original referenced object.
A deep copy of PHP can be done in two ways, 1️⃣clone 2️⃣ the object by serializing it before deserializing it.

RESUMEA));

The __clone method can have no parameters and it automatically contains

That two pointers
This refers to the property of the current object, that is, the property of the old object

Classes and objects
Class: A collection of a set of objects that have the same properties and services.
Object: The result of a class instantiation is an object

Construction Method:
Only one construction method can be declared in a class, but a constructor is called every time the object is created, and the method cannot be invoked actively, so it is often used to perform some useful initialization tasks. For example, an attribute is assigned an initial value when creating an object.

Destructors
Do some of the current operations or perform some functions before destroying a class
Destructors are removed from all references to an object or executed when an object is explicitly destroyed, that is, the object calls the destructor before it is destroyed in memory

The class is actually placed in memory as a stack, so when the destructor is last called, it follows the principle of LIFO

The three main features of object-oriented programming:
Encapsulating inherited polymorphism
Encapsulation: Combine all the attributes of an object with all the services to form an indivisible independent unit (object), information hiding, that is, hiding the inner details of the object as much as possible.
Inherited single inheritance (a derived class in PHP java,c++ can derive from more than one base class) defines a new data type that not only has the newly defined member, but also has an old member. We call the existing classes used to derive new classes as base classes, also known as parent classes, and superclass classes. A new class derived from an existing class is called a derived class, also known as a subclass.

PHP overloading refers to a subclass that overrides a method already in the parent class

Final defines attribute members, not quilt class overrides

Static and const
Static describes the member properties and the member methods are statically,
Static members can restrict external access, because static members belong to the class, are instances that are not part of any object, and are allocated space the first time the class is loaded, and other classes are inaccessible. Only for the strength of the class to share, to a certain extent, the protection of the class.

Static methods cannot be accessed by static members using Const to implement

The Const modifier's member property is accessed in much the same way as a static decorated member, using the name of the class, using self in the method, but not using the $ symbol, or using the object to access

Serialization of objects
There are two cases where we have to serialize objects, the first is to serialize an object when it is being transmitted over the network, and the second is to serialize the object when it is written to a file or database.

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

When serializing an object, the __sellp-– (which can hide some member properties at this time) is executed automatically, and deserialization is the method that performs __wakup ()-(which can be assigned at this time).

The __sleep () function does not accept any arguments, but returns an array that contains the properties that need to be serialized. At the end of the contained property will be ignored at serialization, and if there is no __sleep () method, PHP will save all properties.

Related Article

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.