1th Time Intimate Contact PHP5 (2) _php Foundation

Source: Internet
Author: User
Tags exception handling getmessage

After the article, continue to bite the bullet down .... :)
Type indication

In PHP5, you can indicate in the method of a class that its arguments must be instances of a class:

Example 10: Type instances

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

You can see that 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 as "class variables" and "class methods" in OOP.

A "class method" can be invoked when an object is not instantiated
A "class variable" can be accessed when an object is not instantiated (nor does it require an object's method to invoke)

Example 11: Class variables and class methods


<?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 an accepted method of dealing with abnormal 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

<?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
}
?>


As you can see, "Try" represents the place where the code executes and executes the code for the "catch" area when there is an error.
In the catch area you should indicate the object that executed the exception, which would make our structure clearer

Custom Exception Handling

You can define your custom code to catch unusual errors in your program.
Very simply, you just need to inherit an exception class that requires a constructor and a method called GetMessage in this class:

Example 13: Custom Exception classes

<?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 throw an exception with the throw new Weirdproblem ($foo). If an exception occurs in an area such as try{}, PHP5 jumps into
Catch area to throw an exception.

Name space

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

Example 14: namespaces

<?php
Namespace Math {

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

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

Note: In practical applications, you can define classes with the same name in different namespaces to accomplish different tasks (but interfaces are the same)
< translation complete >



Desperate hunt stand eaves hand poorly translated, some of the incorrect translation of the place, such as terminology, the understanding of the original text, please point out, common perfect, together to start the PHP5 of learning, discussion ...

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.