Ec (2); Type indication everyone knows that 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. <? PhpinterfaceFoo {fu script ec (2); script
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.
<? 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 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:
<? Php
Function foo (ClassName $ object ){
//...
}
?>
Equivalent:
<? Php
Function foo ($ object ){
If (! ($ Object instanceof ClassName )){
Die ("Argument 1 must be an instance of ClassName ");
}
}
?>
Final keywords
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:
<? Php
Class Foo {
Final function bar (){
//...
}
}
?>
Final class:
<? Php
Final class Foo {
// Class definition
}
// The following line is incorrect.
// Class Bork extends Foo {}
?>
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.