4) ★ Class Type Hints type indication
Everyone knows that PHP is a weak type of language. Do not need to be defined before using variables, no data type of the variable is required. This brings a lot of convenience in programming, but also has some hidden dangers, especially when the variable is changed. In PHP5 adds a type indication, you can automatically determine the parameter type of the class method during the execution process. This is similar to RTTI in Java2, with reflection, allow us to control objects well.
php 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 strong type language, the type of all variables will be checked at compile, and the type instructions in PHP are used to check the type of checks. If the type of class method parameters is wrong, an error message similar to "Fatal Error: Argument 1 Must IMPLEMENT INTERFACE BAR ..." will be reported.
The following code:
PHP FUNCTION FOO (ClassName $ Object) {// ...}?>
Equivalent to:
PHP Function Foo ($ Object) {IF ($ Object InstanceOf ClassName) {Die ("Argument 1 Must Be an Instance Of ClassName);}}?>
5) ★ Final Final Keyword
PHP5 newly added Final keywords, which can be added before class or class method. The class method identified as final, and cannot be overwritten in the subclass. The class identified as final cannot be inherited, and the method is default that the final type.
Final method:
php class foo {finction bar () {// ...}}?>
Final class:
PHP Final Class Foo {// Class Definition} // This line is the wrong // class bork eXtends foo {}?>
6) ★ Objects cloning object replication
The previous memory management part said that the object is passed by the reference in PHP5. Objects represented by methods using $ Object2 = $ Object1 are interrelated. If we do need to copy a value that is the same object, it is desirable that the target object is not associated with the source object (like a regular variable to pass the value), then the Clone keyword needs to be used. If you also want to change some of the sources in the source object while copying, you can set a __clone () function, join the operation.
php // object copy 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 a exact identical object.
Then remove the comment of the function of function __clone (), re-run the program. A object that is substantially identical, but partial property changes.
8) ★ Class Constants
CONST keywords can be used in PHP5 to define class constants.
php class foo {constant = "constant";} echo "foo :: constant =". foo :: constant. "/ n";?>
11) ★ __Method__ constant __method__ constant
__Method__ is the new "magic" constant in php5, indicating the name of the class method.
Magic constants is a PHP predefined constant, and its value can be changed. Other existing magic constants in PHP have __line __, __ file __, __ function __, __ class__, etc..
php class foo {echo __method__;}} foo :: show (); // outputs foo: show bar: show (); // outputs foo :: show Either Since__Method__i// compile-time evatated token function test () {echo __method__;} test (); // outputs test?>
Reference:
1. PHP official manual in PHP5 related sections:
http://www.php.net/manual/en/language.oop5.php
http://www.php.net/manual/en/migration5.oop.php
Unfortunately, there are still many chapters in the official manual.