It is suggested that PHP is a weak language. You do not need to define the variables before applying them. you do not need to declare the data type of the variables. This brings a lot of convenience in programming, but also brings some hidden risks, especially when the type of the variable changes. In the type instigation
As we all know, PHP is a weak language. You do not need to define the variables before applying them. you do not need to declare the data type of the variables. This brings a lot of convenience in programming, but also brings some hidden risks, especially when the type of the variable changes. In PHP5, the type instigation is added to determine the parameter types of class methods in the performance process. This is similar to the RTTI in Java2. with reflection, we can control objects well.
<? Php
Inte *** ce Foo {
Function a (Foo $ foo );
}
Inte *** ce 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 types of all variables will be checked during compilation, while in PHP, the type check initiated by the application type is generated at runtime. If the type of the class method parameter is incorrect, a message similar to "Fatal error: Argument 1 must implement inte *** ce Bar…" will be reported ..." This error message.
<? Php
Function foo ($ object ){
If (! ($ Object instanceof ClassName )){
Die ('argument 1 must be an instance of classname ');
}
}
?>
Final keywords
The final keyword is added to 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 marked as final cannot be sustained, 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 governance section, PHP5 transfers objects by reference by default. Objects copied by using methods such as $ object2 = $ object1 are correlated. If we do need to copy a value that is the same as the original object and expect that the target object is not associated with the source object (passed through the value as a common variable ), you need to apply the clone keyword. If you still want to change some parts of the source object while copying, you can define a _ clone () function in the class to participate in the control.
<? Php
// Object replication
Class MyCloneable {
Static $ id = 0;
Function MyCloneable (){
$ This-> id = self: $ id;
}
/*
Function _ clone (){
$ This-> address = 'New York ';
$ This-> id = self: $ id;
}
*/
}
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