Next, continue to dig your head down .... :)
Type indication
In PHP5, you can specify in a class method that the parameter must be an instance of the class:
Example 10: type instance
<? Php
Class foo {
// Code...
}
Class bar {
Public function process_a_foo (foo $ foo ){
// Some code
}
}
$ B = new bar ();
$ F = new foo ();
$ B-> process_a_foo ($ f );
?>
You can see that a class name is specified before the variable so that PHP5 knows that the variable will be an instance of the class.
Static Member
Static members and static methods are generally called "class variables" and "class methods" in OOP ".
A "class method" can be called when the object is not instantiated.
A "class variable" can be accessed when the object is not instantiated (and the object method is not required)
Example 11: class variables and class methods
<? Php
Class calculator {
Static public $ pi = 3.14151692;
Static public function add ($ x, $ y ){
Return $ x + $ y;
}
}
$ S = calculator: $ pi;
$ Result = calculator: add (3, 7 );
Print ("$ result ");
?>
* Exception handling
Exception handling is a method recognized in the development language to handle exception errors, such as in JAVA and C ++.
PHP5 uses the "try" and "catch" keywords to catch exceptions.
Example 12: Exception handling
<? Php
Class foo {
Function divide ($ x, $ y ){
If ($ y = 0) throw new Exception ("cannot divide by zero ");
Return $ x/$ y;
}
}
$ X = new foo ();
Try {
$ X-> divide (3, 0 );
} Catch (Exception $ e ){
Echo $ e-> getMessage ();
Echo "<br/> ";
// Some catastrophic measure here
}
?>
As you can see, "try" indicates the place where the code is executed. When an error occurs, run the code in the "catch" area.
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