For PHP 5.4 You have to know the _php tutorial

Source: Internet
Author: User
Tags php cli traits
PHP 5.4来, which is another major version upgrade since 5.3. This upgrade was marked by the deletion of some of the functions of the air, resulting in a speed increase of 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 test use, $this pointers that can be used by closures, instantiation of class member access,
PHP 5.4.0 performance has been significantly improved to fix more than 100 bugs. Register_globals, magic_quotes, and safe mode were abolished. 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 no longer need to write meta tags in html, and you don't have to send extra headers for UTF-8 compatibility.

Traits
Traits (horizontal reuse/multiple inheritance) is a set of methods that are structured much like "classes" (but not instantiated), which allows developers to easily reuse methods in different classes. PHP is a single-inheritance language, the subclass can inherit only one parent class, so traits comes.
The best application for traits is that the same functions can be shared among multiple classes. For example, we need to make a website that uses APIs from Facebook and Twitter. We are going to build 2 classes, and if so, we need to write a curl method and copy/paste into two classes. Now no, use traits to reuse the code, and this time really follows the DRY (Don T Repeat yourself) principle.
Copy CodeThe 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 (' 1/users/show.json?screen_name=rasmus ')->name;

Are you clear? Then you can see the simpler example.
Copy CodeThe 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, you do not need to configure HTTPD.CONF to use the Apache kill device, but only one can be used in the command line of the ultra-small Webserver. Thanks to PHP (first thanks to the country), PHP 5.4 built the CLI Web server in this case. (PHP CLI webserver only for development use, declined product use)

For a chestnut (Windows platform):
Step one: Establish the Web root directory, router and index
Create a public_html directory in the root directory of the hard disk (for example, C drive), create a new router.php file in the directory, and paste the following code in:
Copy CodeThe 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
}
?>

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, locate the "include_path" line, and add the c:\public_html (semicolon delimited):
1include_path = ".; C:\php\PEAR; C:\public_html "
Save the exit and see the next step

Step Two: Run Web-server
Switch to the PHP installation directory and knock down the most critical commands-run Web-server
Php-s 0.0.0.0:8080-t C:\public_html router.php
Did you start? Do not close the window if the process shuts down the Web server and then close it.
Open your browser: visit http://localhost:8080/index.php,
Hello cichui.com readers!
See, right, that's it!
Tip 1: You can consider self-built a php-server.bat batch, throw to the desktop after you can double-click the boot.
Tip 2: using 0.0.0.0 instead of localhost, you can guarantee that the extranet will not access your Web serve.

Compact Array Syntax
PHP 5.4 provides you with a streamlined array syntax:
copy code code as follows:
$fruits = array (' Apples ', ' oranges ', ' bananas ');//" old "to
// Learn the array of javascript
$fruits = [' Apples ', ' oranges ', ' bananas '];
Associative array
$array = [
' foo ' = ' bar ',
' bar ' = ' foo '
];

Of course, the old syntax still works, and we have one more choice.
Array member access resolution (array dereferencing*)
does not require a temporary variable to handle an array.
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 this:
$tmp = Explode ("', ' Fang Bin Xin ');
echo $tmp [1]; Bin
Now we can play this:
Echo end (Explode (', ' Fang Bin Xin ')),//Xin
Another example of a premium 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 de-referenced for an array, and the effect is poor. In fact, more accurate translation should be: "The function returns the result of the array member access resolution support", see the official PHP explanation.

$this in the anonymous function
You can now refer to an anonymous function (also called a closure function) through $this in the class instance.
Copy the Code code as follows:
Class Foo
{
function Hello () {
Echo ' Hello cichui! ';
}
function Anonymous ()
{
return function () {
$this->hello (); It's 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 can be used before, it is a bit laborious:
function Anonymous ()
{
$that = $this; $that is now $this
return function () use ($that) {
$that->hello ();
};
}

No matter how the configuration in PHP.ini, Short_open_tag, that is, replace the previous.

Binary Direct Volume support
Octal (Oct), preceded by 0, Hex (hex), front plus 0x, binary (bin), now in front plus 0b.
Echo 0b11111; PHP 5.4 Supports binary
Echo 31; Decimal
Echo 0x1f; Hexadecimal
Echo 037; Octal

function type hints
Starting with PHP 5.1, type hints support objects and arrays, and PHP 5.4 begins to support callable.
Copy the 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! ';}); 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 Timers
The $_server[' request_time_float ' array variable was introduced, with microsecond accuracy (one out of 10,000 seconds, FLOAT type). is useful for statistical script run times:
1echo ' Executed in ', Round (Microtime (true)-$_server[' request_time_float '], 2)

Summary
In summary, this PHP 5.4 upgrade is a lot of changes. It's time to upgrade.

http://www.bkjia.com/PHPjc/328139.html www.bkjia.com true http://www.bkjia.com/PHPjc/328139.html techarticle PHP 5.4来, which is another major version upgrade since 5.3. This upgrade was significantly changed, removing some of the functions of the air, bringing up to 20% faster and less memory to make ...

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