Other features of PHP5.0 object model exploration

Source: Internet
Author: User
Tags class definition object model
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.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

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.