1st close contacts with PHP5 (2 ). 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: Example10: type instance followed by the previous article, continue to top your scalp 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
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
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
Class foo {
Function pide ($ x, $ y ){
If ($ y = 0) throw new Exception ("cannot pide by zero ");
Return $ x/$ y;
}
}
$ X = new foo ();
Try {
$ X-> pide (3, 0 );
} Catch (Exception $ e ){
Echo $ e-> getMessage ();
Echo "\ n
\ N ";
// 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.
In the "catch" area, you should specify the object for execution exceptions, so that our structure can be clearer.
Custom exception handling
You can define codes in the program that you are used.
It is very simple. you only need to inherit an exception class. in this class, you need a constructor and a method called getMessage:
Example 13: custom exception classes
Class WeirdProblem extends Exception {
Private $ data;
Function WeirdProblem ($ data ){
Parent: exception ();
$ This-> data = $ data;
}
Function getMessage (){
Return $ this-> data. "caused a weird exception! ";
}
}
?>
Now you can use "throw new WeirdProblem ($ foo)" to throw an exception. If an exception occurs in an area like try {}, PHP5 will jump
To throw an exception.
Namespace
"Namespace" allows you to easily call a group of classes or methods:
Example 14: namespace
Namespace Math {
Class Complex {
//... Code...
Function _ construct (){
Print ("hey ");
}
}
}
$ M = new Math: Complex ();
?>
Note: in practice, you can define classes with the same name in different namespaces to complete different tasks (but the interfaces must be the same)
<翻译完毕>
In the last day, the translation was completed in a bad way. some of the translation problems were incorrect, such as terms and understanding of the original text. Please note that we should work together to improve the learning and discussion of PHP5...
Bytes .... :) Type indication in PHP5, you can specify in a class method that its parameter must be an instance of the class: Example 10: type instance...