A brief summary of important updates for each version of PHP53, 54, 55, and 56 (Part 1)

WBOY
Release: 2016-07-29 09:06:37
Original
2075 people have browsed it

I have nothing to do today. I occasionally saw a brief description of the differences between each version in the version selection section of phpstorm, so I summarized it.

A brief summary of important updates for each version of PHP53, 54, 55, and 56 (Part 1)
Compared with the previous version, the biggest difference between PHP5.3 and PHP5.3 is the namespace and anonymous functions, which are commonly used in daily development and major frameworks, so I won’t go into details.
The main update of PHP5.4 is the array abbreviation syntax format and traits.
Regarding the array abbreviation, it is quite simple to say. It just adds a new declaration method for the array, as follows:

// PHP5.4之前
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// 自 PHP 5.4 起
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
Copy after login

As for the traits of PHP5.4, it is relatively rare. The code description:

class Base {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait SayWorld {
    public function sayHello() {
        parent::sayHello();
        echo 'World!';
    }
}

class MyHelloWorld extends Base {
    use SayWorld;
}

$o = new MyHelloWorld();
$o->sayHello();
Copy after login

Explanation: The result is: 'Hello World!', methods inherited from the base class will be overridden by methods with the same name in the trait. Methods in the current class overwrite members with the same name in the trait; if attributes are defined in the trait, attributes with the same name cannot be defined in the current class; trait Abstract methods and static members can also be defined. Multiple traits can be used, separated by commas, as follows:

use SayHello1,SayHello2;
Copy after login

If there are members with the same name in SayHello1 and SayHello2, a fatal error will occur. The solution is as follows:

use SayHello1,SayHello2 {
    SayHello1::sayHello insteadof SayHello2;  // 意思是用SayHello1中的sayHello方法代替SayHello2中的同名方法,注:此处的sayHello不一定是静态方法
    // ...
    // 或
    SayHello1::sayHello as sayHello1;  // 为其另取一个名字,也可解决
}
Copy after login

The changes in PHP5.5 are mainly in exception handling Add finally keyword and generator generator.
Regarding finally, code description:

try {
    throw new ErrorException('Some Error Message');
} catch (ErrorException $e) {
    echo $e->getMessage()."111 \n";
} catch(Exception $e) {
    echo $e->getMessage()."222 \n";
} finally {
    echo 'finally';
}
Copy after login

Regardless of whether an exception occurs or not, finally will be output.
Regarding the generator, I read the manual, but I don’t know what it is. Got it, plus...
Let’s get here first today...

The above has introduced a brief summary of the important updates of PHP53, 54, 55, and 56 versions (Part 1), including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!