1st close contacts with PHP5 (2) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
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...

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.