Major new features of PHP5.4 official edition

Source: Internet
Author: User
Tags traits
PHP5.4 official version of the important new features of saliva articles, some of the articles have not been fully written. let's take a look at it with me .. currently, the latest version of php is 5.41PHP, which is a very important and convenient development language in the Web development field and is favored by developers. The official version of PHP5.4 is now an important new feature of the official version of PHP 5.4.
Saliva articles, some of which have not yet been fully written. let's take a look at them with me ..
The latest php version is 5.41.
========================================================== ========================

PHP has always been a very important and convenient development language in the Web development field, and is favored by the majority of developers. Now the official version of PHP 5.4 has been released, adding a large number of new features. the official website claims that the performance is improved by 20%, and it consumes less resources. In this article, I will lead you to learn some new features of PHP 5.4.

In PHP 5.4, the first issue is to fix as many as 100 bugs, improve the memory and performance optimization, and remove some earlier versions of methods, such as register_globals, magic_quotes, safe_mode, and so on. Note that the default encoding method in PHP 5.4 has been changed to enable developers to develop multi-language version applications.

Traits introduction

First, we will introduce the newly added Traits in PHP 5.4. In fact, this function is also available in other languages. it can be simply understood as a collection of methods, and it is similar to the class in the organizational structure (but cannot be instantiated like the class ), developers can reuse this method in different classes. Because php is a single-inherited language, a class cannot inherit multiple classes at the same time. At this time, Traits will be used.

Traits is a set of solutions and does not belong to any actual class. You cannot create a Trait instance or directly call methods in Trait. Instead, you must merge Traits into the actual class to use them. In terms of priority, the Trait method will overwrite the inherited methods of the same name, while the methods of the same name in the current merge class will overwrite the Trait method.

The following example describes the usage of Traits. Assume that the APIs of Facebook and Twitter need to be called at the same time in the website we are building. during the call of these two APIs, the curl method must be called for a series of operations, obtain the content returned by the two API interfaces. to not write the same method repeatedly in these two classes, you can use the Traits implementation in php 5.4, as shown in the following code:

/** CURL wrapper trait */

Trait cURL

Public function curl ($ url)

$ Ch = curl_init ();

Curl_setopt ($ ch, CURLOPT_URL, $ url );

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );

$ Output = curl_exec ($ ch );

Curl_close ($ ch );

Return $ output;

/** Twitter API class */

Class Twitter_API

Use cURL; // call Traits

Public function get ($ url)

Return json_decode ($ this-> curl ("http://api.twitter.com/". $ url ));

/** Facebook API class */

Class Facebook_API

Use cURL; // call Traits

Public function get ($ url)

Return json_decode ($ this-> curl ("http://graph.facebook.com/". $ url ));

$ Facebook = new Facebook_API ();

Echo $ facebook-> get ("name; // Here the user name of this facebook account is output by calling the API

/** Demonstrate new features in php 5.4 */

Echo (new Facebook_API)-> get ("name;

$ Foo = "get ";

Echo (new Facebook_API)-> $ foo ("name;

Echo (new Twitter_API)-> get ("name;

In the above code, a feature set is defined by the keyword trait, and its name is Curl, which contains a method named curl, which is based on the url parameter value, call the php built-in cur method to return the page output content corresponding to the url. Then in the Twitter_API class and Facebook_API

Class, respectively use cURL to call this Traits, and in each get method, call the curl method in Traits.

Note: In the above code, apart from using new Facebook_API () to generate facebook objects, we also demonstrated the new features in php 5.4, namely:

It can be a member of the sequence class during instantiation, namely:

Echo (new Facebook_API)-> get ("name;

$ Foo = "get ";

Echo (new Facebook_API)-> $ foo ("name;

Have you seen it? Assign a value to the $ foo variable to get, and then call the get method in the enterprise diagram of the class through (new Facebook_API)-> $ foo ("500058753")-> name; to implement the call.

Let's take another example to illustrate the use of Traits. this example may be simpler, for example, the following code:

Trait Net

Public function net ()

Return "Net ";

Trait Tuts

Public function tuts ()

Return "Tuts ";

Class NetTuts

Use Net, Tuts;

Public function plus ()

Return "+ ";

$ O = new NetTuts;

Echo $ o-> net (), $ o-> tuts (), $ o-> plus ();

Echo (new NetTuts)-> net (), (new NetTuts)-> tuts (), (new NetTuts)-> plus ();

All the above results output NetTuts. In php 5.4, the magic constant of traits is _ TRAIT __.

Built-in debugging server

In the past, php development usually requires cooperation with Apache HTTP Server. In php 5.4, a simple Web server is built in to help developers complete development without complex configurations. The following describes how to use the built-in server in php 5.4 in a windows environment.

Step 1) first create a directory under the root directory of the c drive, which is public_html, and create a router. php file in the file. the code is as follows:

Php

// Router. php

If (preg_match ("# \. php $ #", $ _ SERVER ["REQUEST_URI"])

Require basename ($ _ SERVER ["REQUEST_URI"]); // serve php file

Else if (strpos ($ _ SERVER ["REQUEST_URI"], ".")! = False)

Return false; // serve file as-is

>

Create a simple php file named index. php, as follows:

// Index. php

Echo "Hello Nettuts + Readers! ";

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.