For PHP 5.4 You have to know the _php skills

Source: Internet
Author: User
Tags explode inheritance memory usage php cli safe mode traits

PHP 5.4来, which is another major version upgrade since 5.3. This upgrade is more significant, removing some of the over gas function, bringing speeds up to 20% and less memory usage.

New features and changes
Key new features of this update include: New traits, more streamlined array syntax, built-in webserver for testing, $this pointers to be used for closures, instantiation of class member access,
PHP 5.4.0 performance is greatly improved and fixes more than 100 bugs. Abolished the register_globals, magic_quotes and Safe mode. It is also worth mentioning that Multi-Byte support has been enabled by default, and Default_charset has changed from Iso-8859-1 to UTF-8. Default Send "content-type:text/html; Charset=utf-8 "You don't have to write meta tags in html anymore, and you don't need to send extra headers for UTF-8 compatibility.

Traits
Traits (horizontal reuse/multiple inheritance) is a set of methods that are much like "class" (but not instantiated), allowing developers to easily reuse methods in different classes. PHP is a single inheritance language, subclasses can only inherit a parent class, so traits came.
The best application of traits is that multiple classes can share the same function. For example, we need to make a website that uses the APIs of Facebook and Twitter. We're going to build 2 classes, and if that's the case, we need to write a curl method and copy/paste it into two classes. Now, use traits to reuse code, and this time really follows the DRY (Don T Repeat yourself) principle.

Copy Code code 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
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 (' 1/users/show.json?screen_name=rasmus ')->name;

Do you understand? Then you can look at a simpler example.
Copy Code code 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 you don't use the Apache kill device that needs to be configured httpd.conf, and you just need a small Webserver that you can use on the command line. Thanks to PHP (thanks to the country first), PHP 5.4 built the CLI Web server this time. (PHP CLI webserver only for development use, declined product use)

Give me a chestnut (Windows platform):
Step One: Create the Web root directory, router and index
In the hard disk root directory (such as C disk) to create a public_html directory, the directory to create a new router.php file, the following code copy paste in:
Copy Code code 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
}
?>

To create a new index.php file, 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 the c:\public_html (semicolon-delimited):
1include_path = ".; C:\php\PEAR; C:\public_html "
Save and exit to see the next step

Step Two: Run Web-server
Switch to PHP's installation directory and knock down the most critical command-run Web-server
Php-s 0.0.0.0:8080-t C:\public_html router.php
Did you get started? Do not close the window, if the process closes the Web server and then shuts down.
Open Browser: Access to http://localhost:8080/index.php Bar,
Hello cichui.com readers!
You see that? Yes, that's it!
Tip 1: You can consider to build a php-server.bat batch processing, thrown on the desktop after the double-click can be started.
Tip 2: use 0.0.0.0 instead of localhost to ensure that the extranet does not access your Web serve.

Simplified array syntax for array
PHP 5.4 provides you with a streamlined array array syntax:

Copy Code code as follows:

$fruits = Array (' Apples ', ' oranges ', ' bananas '); "Old" way
Learn the array of JavaScript
$fruits = [' Apples ', ' oranges ', ' bananas '];
Associative arrays
$array = [
' foo ' => ' Bar ',
' Bar ' => ' foo '
];

Of course, the old grammar is still valid, we have one more choice.
Array-member access resolution (array dereferencing*)
Processing an array does not require a temporary variable anymore.
Suppose we need to get the middle name of the 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 this way:
Echo End (Explode (', ' Fang Bin Xin ')); Xin
Another example of a more advanced point:

Copy Code code as follows:

function Foobar ()
{
return [' foo ' => [' Bar ' => ' Hello ']];
}
Echo foobar () [' foo '] [' Bar ']; Hello

* Porcelain Hammer Note: array dereferencing literal translation should be an array of dereference, the effect is poor. In fact more accurate translation should be: "The function returns the result of the array member access resolution support", see PHP official explanation.

$this in anonymous functions
Now, you can refer to an anonymous function (also called a closure function) by $this in the class instance.

Copy Code code as follows:

Class Foo
{
function Hello () {
Echo ' Hello cichui! ';
}
function Anonymous ()
{
return function () {
$this->hello (); It's not going to be 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 can be used before, is a bit laborious:
function Anonymous ()
{
$that = $this; $that is now $this
return function () use ($that) {
$that->hello ();
};
}

Regardless of how the php.ini is configured, Short_open_tag, that is, replaces the previous.

Support Binary Direct Quantity
Octal (Oct), preceded by 0 hexadecimal (hex), preceded by 0x, binary (bin), now in front plus 0b is OK
Echo 0b11111; PHP 5.4 Supports binary
Echo 31; Decimal
Echo 0x1f; Hexadecimal
Echo 037; Octal

function type hint
Since PHP 5.1, type hints support objects and arrays, and PHP 5.4 starts to support callable.

Copy Code code as follows:

function my_function (callable $x)
{
return $x ();
}
function My_callback_function () {return ' Hello cichui! ';}
Class Hello{static function Hi () {return ' Hello cichui! ';}}
Class Hi{function Hello () {return ' Hello cichui! ';}}
Echo my_function (function () {return ' Hello cichui! ';}); Closed-pack 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 introduces the $_server[' request_time_float '] array variable, microsecond precision (one out of 10,000 seconds, FLOAT type). Useful for statistical script running times:
1echo ' executed in ', round (Microtime (true)-$_server[' request_time_float ', 2)

Summary
In short, the PHP 5.4 upgrade made a lot of changes. It's time to upgrade.

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.