Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the new features of PHP 5.4

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains what are the new features of PHP 5.4. the content of the explanation is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn what are the new features of PHP 5.4.What new features are there?

Here comes PHP 5.4.This is another major upgrade since 5.3. This upgrade has made significant changes, removing some out-of-date functions, resulting in up to 20% speed improvement and less memory usage.

New features and changes

Key new features of this update include: new traits, more concise Array array syntax, built-in webserver for testing, $this pointer that can be used in closures, and access by instantiated class members

The performance of PHP 5.4.0 has been greatly improved, fixing more than 100 bug. Abolish register_globals, magic_quotes and security mode. It is also worth mentioning that multibyte support has been enabled by default, and default_charset has changed from ISO-8859-1 to UTF-8. Send "Content-Type: text/html; charset=utf-8" by default, and you no longer need to write meta tag in HTML or send extra header for UTF-8 compatibility.

Traits

Traits (horizontal reuse / multiple inheritance) is a set of methods that are structured much like "classes" (but cannot be instantiated), allowing developers to easily reuse methods in different classes. PHP is a single inheritance language, and subclasses can inherit only one parent class, so Traits comes.

The best use of Traits is that multiple classes can share the same function. For example, if we want to build a website, we need to use Facebook and Twitter's APIs. We are going to build two classes, and if it was before, we need to write a cURL method and copy / paste into the two classes. Not now, use Traits to reuse code, this time really following the DRY (Don't Repeat Yourself) principle.

The copy code is as follows:

/ * * 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; / / use trait here

Public function get ($url)

{

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

}

}

/ * * Facebook API Class * /

Class Facebook_API

{

Use cURL; / / and here

Public function get ($url)

{

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

}

}

$facebook = new Facebook_API ()

Echo $facebook- > get ('500058753')-> name; / / Rasmus Lerdorf

/ * * Now demonstrating the awesomeness of PHP 5.4 syntax * /

Echo (new Facebook_API)-> get ('500058753')-> name

$foo = 'get'

Echo (new Facebook_API)-> $foo ('500058753')-> name

Echo (new Twitter_API)-> get ('1Universe _ users _ Rasmus`)-> name

Do you understand? No? Then take a look at a simpler example.

The copy code is as follows:

Trait Hello

{

Public function hello ()

{

Return 'Hello'

}

}

Trait Cichui

{

Public function cichui ()

{

Return 'cichui'

}

}

Class HelloCichui

{

Use Hello, Cichui

Public function the_end ()

{

Return'!'

}

}

$o = new HelloCichui

Echo $o-> hello (), $o-> cichui (), $o-> the_end ()

Echo (new Hello)-> hello (), new Cichui-> cichui (), (new HelloCichui)-> the_end ()

Built-in Web-Sever

In Web development, Apache HTTPD is the best partner for PHP. Sometimes, when you develop, you don't need an apache mass killer with httpd.conf, but just a mini Webserver that can be used on the command line. Thanks to PHP (thanks to the country first), PHP 5.4 built-in CLI Web server this time. (PHP CLI webserver is for development only, product use is declined)

Take Chestnut (windows platform):

Step 1: establish the web root directory, Router and Index

Create a public_html directory in the root directory of the hard disk (such as C disk), create a new router.php file in the directory, and copy and paste the following code into it:

The copy code is as follows:

/ / 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 new index.php file and copy and paste the following code:

/ / index.php

Echo 'Hello cichui.com readers'

? >

Edit your php.ini file, find the "include_path" line, and add c:\ public_html (separated by semicolons):

1include_path = ".; C:\ php\ PEAR;C:\ public_html"

Save and exit, see the next step

Step 2: run Web-Server

Change to the installation directory of php and type the most critical command-run Web-server

Php-S 0.0.0.0 8080-t C:\ public_html router.php

Has it started yet? Do not close the window. If the process closes, so does the Web server.

Open the browser: visit http://localhost:8080/index.php

Hello cichui.com Readers!

Did you see that? Yeah, that's it!

Tip 1: you can consider creating a batch of php-server.bat that you can double-click to start after throwing it on the desktop.

Tip 2: use 0.0.0.0 instead of localhost to ensure that your web serve will not be accessed by the external network.

Simplified Array array syntax

PHP 5.4 presents you with a concise array array syntax:

The copy code is as follows:

$fruits = array ('apples',' oranges', 'bananas'); / / "old" way

/ / learn the array of Javascript.

$fruits = ['apples',' oranges', 'bananas']

/ / associative array

$array = [

'foo' = >' bar'

'bar' = >' foo'

]

Of course, the old grammar is still valid, and we have one more choice.

Array member access parsing (Array dereferencing*)

Temporary variables are no longer needed to process arrays.

Suppose we need to get the middle name of Fang Bin Xin

Echo explode ('', 'Fang Bin Xin') [1]; / / Bin

Before PHP 5.4, we need to do this:

$tmp = explode ('', 'Fang Bin Xin')

Echo $tmp [1]; / / Bin

Now, we can play like this:

Echo end (explode (', 'Fang Bin Xin')); / / Xin

Let's take a more advanced example:

The copy code is as follows:

Function foobar ()

{

Return ['foo' = > [' bar' = > 'Hello']]

}

Echo foobar () ['foo'] [' bar']; / / Hello

* porcelain hammer note: the literal translation of Array dereferencing should dereference the array, but the effect is not good. In fact, a more accurate translation should be: "support for accessing and parsing the array members of the results returned by the function", see the official explanation of PHP for details.

$this in anonymous functions

Now, you can reference an anonymous function (also known as a closure function) through $this in the class instance.

The copy code is as follows:

Class Foo

{

Function hello () {

Echo 'Hello Cichuifang'

}

Function anonymous ()

{

Return function () {

$this- > hello (); / / it was impossible to play like this before.

}

}

}

Class Bar

{

Function _ _ construct (Foo $o) / / object of class Foo typehint

{

$x = $o-> anonymous (); / / get Foo::hello ()

$x (); / / execute Foo::hello ()

}

}

New Bar (new Foo); / / Hello Cichui!

In fact, it used to be able to make do with it, but it was a bit of an effort:

Function anonymous ()

{

$that = $this; / / $that is now $this

Return function () use ($that) {

$that- > hello ()

}

}

No matter how it is configured in php.ini, short_open_tag replaces the previous one.

Support for binary direct quantities

Octal (oct), preceded by 0; hexadecimal (hex), preceded by 0x; binary (bin), now preceded by 0b

Echo 0b11111; / / PHP 5.4supports binary.

Echo 31; / / Decimal

Echo 0x1f; / / hexadecimal

Echo 037; / / Octal

Function type hint

Since PHP 5. 1, type prompts support objects and arrays, and PHP 5. 4 supports callable.

The copy code is as follows:

Function my_function (callable $x)

{

Return $x ()

}

Function my_callback_function () {return 'Hello Cichuiroads;}

Class Hello {static function hi () {return 'Hello Cichuiroads;}}

Class Hi {function hello () {return 'Hello Cichuiroads;}}

Echo my_function (function () {return 'Hello Cichuifolds;}); / / closure function

Echo my_function ('my_callback_function'); / / callback function

Echo my_function (['Hello',' hi']); / / Class name, static method

Echo my_function ([(new Hi), 'hello']); / / Class name, method name

High precision timer

This time the $_ SERVER ['REQUEST_TIME_FLOAT'] array variable is introduced with microsecond precision (1/1000000 seconds, float type). It can be useful for counting the running time of scripts:

1echo 'Executed in', round (microtime (true)-$_ SERVER ['REQUEST_TIME_FLOAT'], 2)

Thank you for your reading, the above is the content of "what are the new features of PHP 5.4". After the study of this article, I believe you have a deeper understanding of the new features of PHP 5.4. the specific use of PHP 5.4 also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report