Home >Backend Development >PHP Tutorial >PHP5 trial (2)_PHP tutorial
Abstract class
Abstract class cannot be instantiated.
Abstract classes, like other classes, allow variables and methods to be defined.
An abstract class can also define an abstract method. The method of the abstract class will not be executed, but it may be executed in its derived class.
Example 6: Abstract class
abstract class foo {
protected $x;
abstract function display();
function setX( $x) {
$this->x = $x;
}
}
class foo2 extends foo {
function display() {
// Code
}
}
?>
__call
PHP5 object has a new special method __call(), which is used to monitor the Other methods. If you try to call a method that does not exist on the object, the __call method will be called automatically.
Example 7: __call
class foo {
function __call($name,$arguments) {
print("Did you call me? I'm $name!");
}
} $x = new foo();
$x->doStuff();
$x->fancy_stuff();
?>
This special method can be used to implement "overloading" actions, so that you can check your parameters and pass them by calling a private method.
Example 8: Using __call to implement "overload" action
class Magic {
function __call($name,$arguments) {
if ($name=='foo') {
if(is_int($arguments[0])) $this->foo_for_int($arguments[0]);
if(is_string($arguments[0] )) $this->foo_for_string($arguments[0]);
}
} private function foo_for_int($x) {
print("oh an int!");
} private function foo_for_string($x) {
print("oh a string!");
}
} $x = new Magic();
$x->foo(3);
$x->foo("3");
?>
__set and __get
This is a great method, __set and _ The _get method can be used to capture variables and methods that do not exist in an object.
Example 9: __set and __get
class foo {
function __set($name,$val) {
print("Hello, you tried to put $val in $name");
}
function __get($name) {
print("Hey you asked for $name");
}
}
$x = new foo();
$x->bar = 3;
print($x->winky_winky);
?>
Type Instructions
In PHP5, you can specify in an object's method that its parameter must be an instance of another object.
Example 10: Type indication
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, we can explicitly specify the name of an object before the parameter, and PHP5 will recognize that the parameter will be an object. Example.
Static members
Static members and static methods are called "object methods (class methods)" and "object variables (class variables)" in the terminology of object-oriented programming .
"Object method" is allowed to be called before an object is instantiated. Similarly, "object variables" can be controlled independently before an object is instantiated (without using an object's methods to control it).
Example 11: Object methods and object variables
class calculator {
static public $pi = 3.14151692;
static public function add($ x,$y) {
return $x + $y;
}
}
$s = calculator::$pi;
$result = calculator::add(3,7 ; This concept exists in both Java and C++. We are pleased to see that this application has been added to PHP5. You can try using "try" and "catch" to control program errors.
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
}
?>
In the above example, we use "try" to execute the statement in curly brackets when an error occurs , the code will hand over the error to the "catch" clause for handling. In the "catch" clause, you need to specify that the error is to be handed over to an object for handling. This can make the code structure look clearer, because now we All error messages can be handed over to an object for processing.
Custom error handling
You can easily use custom error handling code to control accidents in your program. You just need to derive your own error control class from the exception class. In your own error control class, you need to have a constructor and a getMessage method. The following is an example.
Example 13: Custom error handling
class WeirdProblem extends Exception {
private $data;
function WeirdProblem($data) {
parent::exception();
$this->data = $data;
}
function getMessage() {
return $this->data . " caused a weird exception !";
}
}
?>
Now we can use "throw new WeirdProblem($foo)" to throw an error handler if the error is in "try" Occurs in the code block, PHP5 will automatically hand over the error to the "catch" part for handling.
Namespace
Namespace is useful for grouping classes or grouping functions. It can group some related classes or functions together for easy calling later.
Example 14: Namespace
namespace Math {
class Complex {
//...code...
function __construct() {
print("hey");
}
}
} $m = new Math::Complex();
?>
Note Under what circumstances do you need to use namespaces? In actual application, you may need to declare two or more objects with the same name to do different things, then you can put them in different namespaces. Go (but the interface is the same).
http://www.bkjia.com/PHPjc/313864.html
www.bkjia.com