* This article is a supplement and revision to the Classes and Objects in PHP5 series, and introduces the overall framework of the PHP5 object system, but some features are not specifically described. It is strongly recommended to read this article after reading "Classes and Objects in PHP5".
PHP5 launched the object system is believed to be the most expected of us. PHP5 draws on the Java2 object model, provides a more powerful object-oriented programming support, using PHP to achieve OO will be easy and natural.
Object passing
PHP5 uses Zend Engine II, objects are stored in a separate structure object store, not stored in zval like other general variables (stored in Zval as objects in PHP4 and general variables). Only pointers to objects, not content (value), are stored in zval. When we copy an object or pass an object as a parameter to a function, we do not need to copy the data. Just keep the same object pointer and be notified by another zval now that this particular object is pointing to the object Store. Since the object itself is in object Store, any changes we make to it will affect all zval structures that hold the pointer to the object----in the program that any change to the target object will affect the source object: This makes the PHP object look like always by reference (reference ), so the object in PHP is passed by "reference" by default, and you no longer need to declare it using & As in PHP4.
Garbage collection mechanism
Some languages, most typically such as C, require you to explicitly request the allocation of memory when you create a data structure. Once you have allocated memory, you can store the information in the variable. You also need to release the memory at the end of the use of the variable, which allows the machine to empty out memory to other variables and avoid consuming light memory.
PHP can automate memory management to remove objects that are no longer needed. PHP uses the simple garbage collection (garbage collection) mechanism of the reference count (reference counting). Each object contains a reference counter, each reference connected to the object, and the counter is incremented by 1. When reference leaves the living space or is set to NULL, the counter is reduced by 1. When a reference counter for an object is zero, PHP knows that you will no longer need to use this object to free up the memory space it occupies.
For example:
Class person{
}
function Sendemailto () {
}
$haohappy = new Person ();
Create a new object: reference count Reference count = 1
$haohappy 2 = $haohappy;
Copy by reference: Reference count = 2
Unset ($haohappy);
Delete a reference: Reference count = 1
Sendemailto ($haohappy 2);
To pass an object by reference:
During function execution:
Reference count = 2
When execution is complete:
Reference count = 1
Unset ($haohappy 2);
Delete reference: Reference count = 0 automatically frees memory space
?>
The above is PHP5 in memory management changes, perhaps people are not interested. Let's look at the specific differences between the object model and the PHP4 in PHP5:
★ New Features
★ Improved Features
1) ★private and Protected members private and protected class member (properties, methods)
2) ★abstract Classes and Methods abstract classes and abstract methods
3) ★interfaces Interface
4) ★class type Hints indication =
5) ★final Final keyword =
6) ★objects Cloning object copy =
7) ★constructors and destructors constructors and destructors
8) ★class Constants Class constant =
9) ★exceptions Exception handling
10) ★static member Static class member
11) ★__method__ constant __METHOD__ constant =
12) ★reflection Reflection mechanism
1th, 2, 3, 7, 10 Please check this article at the end of the "Classes and Objects in PHP5" series, which has been described in detail, this article is no longer explained. 9th exception Handling and 12th reflection mechanism content is more abundant, limited to the length is not in the article, please pay attention to the upcoming "PHP & More" E-magazine, the second issue, will be specifically introduced.
The following are some of the 4th, 5, 6, 8, and 11 language features:
4) ★class Type Hints indication
As we all know, PHP is a weak type of language. You do not need to define a variable before you can use it, and you do not need to declare the variable's data type. This brings a lot of convenience in programming, but it also brings some pitfalls, especially when the types of variables change. The type indication is added to the PHP5, and the parameter types of the class method can be automatically judged during execution. This is similar to the Rtti in Java2, which allows us to control objects well with reflection.
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 are checked at compile time, while the check of types using type indications in PHP occurs at run time. If the class method arguments are of the wrong type, an error message similar to "Fatal Error:argument 1 must implement Interface Bar ..." is reported.
The following code:
function foo (ClassName $object) {
// ...
}
?>
Equivalent:
function foo ($object) {
if (! ( $object instanceof ClassName)) {
Die ("Argument 1 must is an instance of ClassName");
}
}
?>
5) ★final Final keyword
The final keyword is added in PHP5, which can be added before a class or class method. A class method that is identified as final and cannot be overwritten in a subclass. A class that is identified as final, cannot be inherited, and its methods are default to the final type.
Final method:
Class Foo {
Final function bar () {
// ...
}
}
?>
Final class:
Final class Foo {
Class definition
}
The following line is wrong
Class Bork extends Foo {}
?>
6) ★objects Cloning object copy
As previously mentioned in the Memory Management section, the object is passed by reference by default in PHP5. Objects such as those copied using $object2= $object 1 are interrelated. If we do need to replicate a value that is the same as the original object and want the target object to be unrelated to the source object (passed by value like a normal variable), then you need to use the Clone keyword. If you also want to change some parts of the source object while copying, you can set a __clone () function in the class to join 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 + +;
}
*/
}
$obj = new Mycloneable ();
$obj->name = "Hello";
$obj->address = "Tel-aviv";
Print $obj->id. "\ n";
$obj _cloned = Clone $obj;
Print $obj _cloned->id. "\ n";
Print $obj _cloned->name. "\ n";
Print $obj _cloned->address. "\ n";
?>
The above code copies an identical object.
Then please remove the comment function __clone () and rerun the program. It will copy an object that has the same basic but partial property change.
8) ★class Constants class constants
The Const keyword can be used to define class constants in PHP5.
Class Foo {
CONST CONSTANT = "constant";
}
echo "foo::constant =". Foo::constant. "\ n";
?>
11) ★__METHOD__ Constant __METHOD__ constant
__method__ is a new "magic" constant in PHP5 that represents the name of a class method.
The magic constant is a PHP predefined constant, its value can be changed, PHP's other existing magic constants have __line__, __file__, __function__, __class__ and so on.
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
function Test () {
Echo __method__;
}
Test (); Outputs test
?>
(Source: Viphot)
http://www.bkjia.com/PHPjc/314036.html www.bkjia.com true http://www.bkjia.com/PHPjc/314036.html techarticle * This article is a supplement and revision to the Classes and Objects in PHP5 series, and introduces the overall framework of the PHP5 object system, but some features are not specifically described. It is strongly recommended that you read the " ...