* This article supplements and revises the ClassesandObjectsinPHP5 series and introduces the overall framework of the PHP5 object system. However, some features are not described in detail. We strongly recommend that you read this article after reading ClassesandObjectsinPHP5. PHP5's object system is expected most. PHP5 draws on the object model of Java2 and provides * This article supplements and revises the articles in the Classes and Objects in PHP5 series, and introduces the overall framework of the PHP5 object system, however, some features are not described in detail. We strongly recommend that you read this article after reading "Classes and Objects in PHP5.
PHP5's object system is expected most. PHP5 draws on the object model of Java2 and provides powerful object-oriented programming support. using PHP to implement OO makes it easy and natural.
Object transfer
PHP5 uses Zend Engine II, and objects are stored in an independent structure Object Store, unlike other common variables stored in Zval (objects in PHP4 are stored in Zval like General variables ). In Zval, only the object pointer is stored, not the content (value ). When we copy an object or pass an object as a parameter to a function, we do not need to copy data. Only keep the same Object pointer and the other zval notifies the Object Store to which the specified Object points. Because the Object is located in the Object Store, any changes we make to it will affect all the zval structures that hold the object pointer-any changes to the target object in the program will affect the source object .. This makes PHP Objects look like they are always passed through reference, so objects in PHP are passed through reference by default, you no longer need to use & As in PHP4.
Garbage collection mechanism
Some languages, such as C, require you to explicitly allocate memory when you create a data structure. Once allocated to the memory, you can store information in the variable. At the same time, you also need to release the memory when you stop using the variable, so that the machine can leave the memory to other variables to avoid consumption of memory.
PHP can automatically perform memory management to clear unnecessary objects. PHP uses reference counting, a simple garbage collection mechanism. Each object contains a reference counter. each reference is connected to an object, and the counter is incremented by 1. When the reference leaves the living space or is set to NULL, the counter minus 1. When the reference counter of an object is zero, PHP knows that you no longer need to use this object, releasing the memory space occupied by it.
For example:
Class Person {
}
Function sendEmailTo (){
}
$ Haohappy = new Person ();
// Create a new object: Reference count = 1
$ Haohappy2 = $ haohappy;
// Copy by Reference: Reference count = 2
Unset ($ haohappy );
// Delete a Reference: Reference count = 1
SendEmailTo ($ haohappy2 );
// Pass the object through reference:
// During function execution:
// Reference count = 2
// After execution:
// Reference count = 1
These are changes in memory management of PHP5, which may be of little interest to you. Let's take a look at the specific differences between the object model and PHP4 in PHP5:
★New Feature
★Improved functions
1)★Private and Protected Members Private and protection class Members (attributes, methods)
2)★Abstract Classes and Methods Abstract Classes and Abstract Methods
3)★Interfaces interface
4)★Class Type Hints Type indication =
5)★Final keyword =
6)★Objects Cloning object replication =
7)★Constructors and Destructors constructor and Destructor
8)★Class Constants Class constant =
9)★Exceptions exception handling
10)★Static member Static class member
11)★_ METHOD _ constant =
12)★Reflection mechanism
For details about Classes and Objects in PHP5 at the end of this article, see 1st, 2, 3, 7, and 10. The 9th-point exception handling and 12th-point reflection mechanisms are rich in content and are not described in this article. please pay attention to the second issue of the upcoming PHP & More e-magazine, which will be specially written for introduction.
The following describes the language features at, and:
4)★Class Type Hints Type indication
As we all know, PHP is a weak language. You do not need to define the variable before using it, and do not need to declare the data type of the variable. This brings a lot of convenience in programming, but it also brings some hidden risks, especially when the type of the variable changes. The type indication is added in PHP5 to automatically judge the parameter type of the class method during execution. This is similar to the RTTI in Java2. with reflection, we can control objects well.
Interface Foo {
Function a (Foo $ foo );
}
Interface Bar {
Function B (Bar $ bar );
}
Class FooBar implements Foo, Bar {
Function a (Foo $ foo ){
//...
}
Function B (Bar $ bar ){
//...
}
}
$ A = new FooBar;
$ B = new FooBar;
$ A-> a ($ B );
$ A-> B ($ B );
?>
In a strongly typed language, the types of all variables will be checked during compilation, while in PHP, type indication is used to check the types occurs during runtime. If the type of the class method parameter is incorrect, a message similar to "Fatal error: Argument 1 must implement interface Bar…" will be reported ..." This error message.
Run the following code:
Function foo (ClassName $ object ){
//...
}
?>
Equivalent:
Function foo ($ object ){
If (! ($ Object instanceof ClassName )){
Die ("Argument 1 must be an instance of ClassName ");
}
}
?>
5)★Final keyword
The final keyword is added in PHP5, which can be added before the class or class method. The class method identified as final. it cannot be overwritten in the subclass. The class identified as final cannot be inherited, and all methods in the class are of the final type by default.
Final method:
Class Foo {
Final function bar (){
//...
}
}
?>
Final class:
Final class Foo {
// Class definition
}
// The following line is incorrect.
// Class Bork extends Foo {}
?>
6)★Objects Cloning object replication
As mentioned earlier in the memory management section, PHP5 transfers objects by reference by default. Objects copied using methods such as $ object2 = $ object1 are correlated. If we do need to copy an object with the same value as the original object and want the target object to be not associated with the source object (passed by values as common variables), we need to use the clone keyword. If you want to change some parts of the source object while copying, you can define a _ clone () function in the class and add the operation.
// Object replication
Class MyCloneable {
Static $ id = 0;
Function MyCloneable (){
$ This-> id = self: $ id ++;
}
/*
Function _ clone (){
$ This-> address = "New York ";
$ This-> id = self: $ id ++;
}
*/
}
Print $ obj_cloned-> id. "\ n ";
Print $ obj_cloned-> name. "\ n ";
Print $ obj_cloned-> address. "\ n ";
?>
The above code copies a completely identical object.
Remove the comments of the function _ clone () and re-run the program. An object that is basically the same but has some property changes will be copied.
8)★Class Constants Class constant
In PHP5, you can use the const keyword to define class constants.
Class Foo {
Const constant = "constant ";
}
Echo "Foo: constant =". Foo: constant. "\ n ";
?>
11)★_ METHOD _ constant
_ METHOD _ is a new "Magic" constant in PHP5, indicating the name of the class METHOD.
A magic constant is a PHP predefined constant whose values can be changed, other existing magic constants in PHP include _ LINE _, _ FILE _, _ FUNCTION _, and _ CLASS.
Class Foo {
Function show (){
Echo _ METHOD __;
}
}
Class Bar extends Foo {
}
Foo: show (); // outputs Foo: show
Bar: show (); // outputs Foo: show either since _ METHOD _ is
// Compile-time evaluated token
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.