1th Time Intimate Contact PHP5 (2) _php tutorial

Source: Internet
Author: User
Next, continue to bite the bullet down .... :)
Type indication

In PHP5, you can indicate in a method of a class that its arguments 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, specifying a class name before the variable lets PHP5 know that the variable will be an instance of a class

Static members

Static members and static methods are commonly referred to in OOP as "class variables" and "class methods."

A "class method" can be invoked when the object is not instantiated
A "class variable" can be accessed when the object is not instantiated (and does not require the method of the object to invoke)

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 an accepted method of handling exception errors in the development language, 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
}
?>


As you can see, "Try" represents the place where the code executes, and the code that executes the "catch" area when there is an error.
In the "Catch" area you should specify the object that executes the exception, which 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, in which you need a constructor and a method called GetMessage:

Example 13: Custom Exception class

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 throw exceptions with "throw new Weirdproblem ($foo)". If an exception occurs in a zone such as try{}, PHP5 will jump in
Catch area to throw an exception.

Name space

namespaces allow you to easily invoke a set of classes or methods:

Example 14: namespaces

Namespace Math {

Class Complex {
.... Code ...
function __construct () {
Print ("Hey");
}
}
}

$m = new Math::complex ();
?>

Note: In real-world applications, you can define classes with the same name in different namespaces to accomplish different tasks (but the interfaces are the same)
<翻译完毕> <翻译完毕>



Voice limp hand lame translated, some translation is not correct place, such as terminology, the understanding of the original, please point out, together to improve, together began PHP5 study, explore ...

http://www.bkjia.com/PHPjc/314327.html www.bkjia.com true http://www.bkjia.com/PHPjc/314327.html techarticle Next , continue to bite the bullet down .... :) type indicates that in PHP5, you can indicate in a method of a class that its argument must be an instance of a 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.