PHP5 Learning (1)

Source: Internet
Author: User
PHP5 study record (1)

First, the main new features of PHP5

The 1.public/private/protected access modifier, for methods and properties in the object.

You can use the generic object-oriented access modifier to control access levels for methods and properties:

Class MyClass {

    Private $id =;    Public Function GetId () {        return $this->id;    }}
?

2. Unified constructor name __construct ().

To avoid the name of the constructor and the name of the class, it is now declared with __construct (), making it easier to transfer classes in the hierarchy of classes:

?

Class MyClass {    function __construct () {        print ' Inside constructor ';    }}

?

? 3. Define the object's Destructor method by __destructor ().

Allows you to define a destructor to unregister an object:

?

Class MyClass {    function __destruct () {        print ' destroying object ';    }}

?

? 4. Interface.

A class that uses an interface can load more than one related interface. A class can inherit from another class only, but it is possible to implement any number of interfaces:

?

Interface Display {    function display ();} Class Circle implements Display {    function dispaly () {        print ' dispalying Circle \ n ';    }}

?

? 5.instanceof operator.

Instanceof is used to detect whether a given object belongs to (inherits from) a class, a subclass of a class, an interface (interface). Returns true if it is.

?

if ($obj instanceof circle) {    print ' $obj is a Circle ';}
?

?

?

6.Final tagging method.

The final keyword allows you to identify a method so that it cannot be overloaded with a quilt class.

?

Class MyClass {    final function getbaseclassname () {        return __class__;    }}
?

?

7.Final Tag class.

After declaring a class as final, it cannot be inherited. The following example will be an error.

?

Final class Finalclass {}class Bogusclass extends Finalclass {}

?

?

8. Force replication of objects

In order to clone an object, you must use the Clone keyword. You can declare a __clone () method in the class, which will be called during the cloning process (called after the properties and methods are copied from the original object):

?

Class Myclas {    function __clone () {        print ' Object is being cloned ';    }} $obj = new MyClass (); $obj _copy = Clone $obj;
?

?

9. Constants in the class.

The class definition now contains constants and can be referenced with a class:

?

Class MyClass {    Const SUCCESS = "SUCCESS";    Const FAILURE = "FAILURE";} Print myclass::success;

?

? 10. Static methods.

You can now define static methods that can be used without instantiating them. Static methods cannot use the $this variable because they are not bound to any specific object:

Class MyClass {

    static function HelloWorld () {        print ' Hello world ';    }} Myclass::helloworld ();

?

11. Static members.

The definition of a class now contains static members (attributes) that can be accessed through the class itself. The most commonly used is the single-piece (Singleton) mode:

?

Class Singleton {    static private $instance = NULL;    Private Function __construct () {    }    static public function getinstance () {        if (Sell:: $instance = = NULL) {
  self:: $instance = new Singleton ();        }        Return self:: $instance;    }}

?

?

12. Abstract class.

Declaring a class as abstract can prevent it from being instantiated. But you can inherit an abstract class:

?

Abstract class MyBaseClass {    function dispaly () {        print ' Default dispaly routine being called ';    }}
?

?

13. Abstract methods.

Declare the method abstract so that it can be defined in the inherited subclass. The class that contains the abstract method must itself be an abstract class:

?

Abstact class MyBaseClass {    abstract function dispaly ();}

?

? 14. Object type hint.

Object type hints can be made to parameters in the function declaration. If the correct object type is not passed when the function is called, the system error:

?

function Expectsmyclass (MyClass $obj) {}

?

? 15. Supports objects returned by a continuous reference method.

In PHP4, you cannot directly refer to the object returned by the method. You must first assign a value to an intermediate variable and then use that variable to refer to it.

PHP 4:

?

$dummy = $obj->method (); $dummy->method2 ();

?

? PHP 5:

?

$obj->method ()->method2 ();

?

?

16. Iteration.

PHP5 allows PHP classes and PHP inheriting classes to implement the iterator interface. After implementing an iterative interface, you can use the foreach () language structure to traverse an instance of a class:

?

$obj = new Myiteratorimplementaion (); foreach ($obj as $value) {    print $value;}

?

?

17.__autoload () method.

The __autoload () function is called automatically when you need to use a class that is not defined. By calling this function, the script's engine provides the opportunity to load the class the last time before PHP throws a class-undefined error:

?

function __autoload ($class _name) {    include_once ($class _name. "PHP");} $obj = new MyClass (); $obj 2 = new MyClass2 ();
?

18. Exception handling.

PHP5 adds an exception handling paradigm for the famous Try/throw/catch architecture. When used, you can only throw objects that inherit from the exception class:

Class SQLException extends Exception {

    public $problem;    function __construct ($problem) {        $this->problem = $problem;    }}     try{... throw new SQLException ("couldn ' t connect to database"); ...} catch (SQLException $e) {     print "SQLException";} catch (Exception $e) {    print ' other Exception ';}

?

The 19.foreach function supports references.

In PHP 4, you cannot traverse an array and change its value at the same time. But PHP 5 can change the value of the array in the loop that iterates through the array by accelerating the & (reference) symbol to the parameters of the foreach () Loop:

?

foreach ($array as & $value) {    if ($value = = = "Null") {        $VALUE = null    }}

?

? 20. Set the default value for the reference parameter.

In PHP 4, you can only set default values for parameters that pass values. PHP 5 now lets you set default values for parameters that pass references:

?

Function My_func (& $arg = null) {    if ($arg = = = null) {        print ' $arg is empty ';    }} My_func ();
?

?

  • 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.