PhP5 trial (2)

Source: Internet
Author: User


Abstract class

Abstract classes cannot be instantiated.
Abstract classes, like other classes, allow definition of variables and methods.
An abstract class can also define an abstract method. An abstract class method is not executed, but may be executed in its derived class.

Example 6: abstract class

<? PHP
Abstract class Foo {
Protected $ X;
Abstract function display ();
Function setx ($ X ){
$ This-> X = $ X;
}
}
Class foo2 extends Foo {
Function display (){
// Code
}
}
?>

_ Call

A special _ call () method is added to the PhP5 object. This method is used to monitor other methods in an object. If you try to call a method that does not exist in an object, the __call method will be automatically called.

Example 7: __call

<? PHP
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 the "overload" action, so that you can check your parameters and PASS Parameters by calling a private method.

Example 8: Use _ call to implement "overload" Action

<? PHP
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. The __set and _ Get methods can be used to capture variables and methods that do not exist in an object.

Example 9: _ set and _ Get

<? PHP
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 indication

In PhP5, you can specify in the object method that the parameter must be an instance of another object.

Example 10: Type indication

<? 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 );
?>

We can see that we can explicitly specify the name of an object before the parameter, PhP5 will identify this parameter as an object instance.

Static Member

Static members and static methods are called "class methods" and "class variables" in terms of face object programming )".
An object method can be called before an object is instantiated. Similarly, "object variables" can be independently controlled before an object is instantiated (an object method is not needed ).

Example 11: Object methods and object variables

<? 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 acceptedProgramThe incorrect ideal method has this concept in Java and C ++. We are delighted to see that PhP5 has already been applied in this field. You can try to use "try" and "catch" to control program errors.

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 "n <br/> N ";
// Some catastrophic measure here
}
?>

In the above example, we used "try" to execute the statement in curly brackets. When an error occurs,CodeErrors will be handed over to the "catch" clause for processing. In the "catch" clause, You need to specify that the error should be handed over to an object for processing. This can make the code structure clearer, because now we can give all the error information to an object for processing.

Custom error handling

You can easily use custom code to handle errors to control exceptions in your program. You only need to assign your own error control class from the exception class. In your own error control class, you need a constructor and a getmessage method. The following is an example.

Example 13: custom error handling

<? PHP
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 handle. If an error occurs in the "try" code block, phP5 automatically submits the error to the "catch" part for processing.

Namespace

Namespaces are useful for grouping classes or function groups. It can combine some related classes or functions to facilitate future calls.

Example 14: namespace

<? PHP
Namespace math {
Class Complex {
//... Code...
Function _ construct (){
Print ("hey ");
}
}
} $ M = new math: complex ();
?>

Note the situations in which you need to use namespaces. In actual use, you may need to declare two or more objects with the same name for different purposes, then you can put them in different namespaces (but the interfaces must be the same ).

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.