1th Time Intimate Contact PHP5 (2)

Source: Internet
Author: User
PHP5 connect the article, continue to bite the bullet down .... :)


Type indication







in PHP5, you can indicate in the method of a class that the argument must be an instance of a class:





Example 10: type instance





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


?>





can see that by specifying a class name before the variable, let 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 (and does not 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


}


?>








can see that "Try" represents the place where the code executes and executes the code of 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.


is very simple, you just need to inherit an exception class that requires a constructor and a method called GetMessage in this class:





Example 13: Custom Exception class





<?php


class Weirdproblem extends Exception {





private $data;





function Weirdproblem ($data) {


parent::exception ();


$this->data = $data;


    }





function GetMessage () {


return $this->data. "caused a weird exception!";


    }


}


?>





can now throw an exception using the throw new Weirdproblem ($foo). If an exception occurs in an area such as try{}, PHP5 jumps into the


the catch area to throw an exception.





Name space







namespace allows you to easily invoke a set of classes or methods:





Example 14: namespace





<?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 will be the same)


< translation finished >











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 begin the PHP5 study, explore ...








er Quan


2003/7/2 in Zhengzhou





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.