Home > Article > Backend Development > The first intimate contact with PHP5(2)_PHP tutorial
Continuing from the previous article, continue scrolling down. . . . :)
Type indication
In PHP5, you can specify in a class method that its parameter must be an instance of a 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);
?>
As you can see, specify a class name before the variable to let PHP5 know that this variable will be an instance of a class
Static members
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 does not require an object method to be called)
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 recognized method of handling exception errors in development languages, such as in JAVA and C++.
PHP5 uses the "try" and "catch" keywords to catch exceptions.
Example 12: Exception handling
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 " n
n";
// Some catastrophic measure here
}
?>
It can be seen that "try" means executing code where the code in the "catch" area is executed when an error occurs.
In the "catch" area you should specify the object that executed the exception, this will make our structure clearer
Custom exception handling
You can define your own custom code for catching exception errors in your program.
Very simple, you just need to inherit an exception class, which requires a constructor and a method called getMessage:
Example 13: Custom exception class
< ;?php
class WeirdProblem extends Exception {
private $data;
function WeirdProblem($data) {
parent::exception();
$this- >data = $data;
}
function getMessage() {
return $this->data . " caused a weird exception!";
}
}
?>
You can now use "throw new WeirdProblem($foo)" to throw exceptions. If an exception occurs in an area like try{}, PHP5 will jump into the
catch area to throw the exception.
Namespace
"Namespace" allows you to conveniently 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 actual applications, you can define classes with the same name in different namespaces To complete different tasks (but the interface should be the same)
The translation was finished lamely all day long. Some of the translations are incorrect, such as terminology, Please point out your understanding of the original text one by one, improve it together, and start learning and discussing PHP5 together. . .