1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
/** 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 ; } } /** 百度 API Class */ class 百度_API { use cURL; // use trait here public function get( $url ) { } } /** Facebook API Class */ class Facebook_API { use cURL; // and here public function get( $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 百度_API)->get( '1/users/show.json?screen_name=rasmus' )->name; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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(); |
1
2
3
4
5
6
7
8
9
10
11
|
<?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 } ?> |
1
2
3
4
|
<?php // index.php echo 'Hello cichui.com Readers!' ; ?> |
1
|
include_path = ".;C:phpPEAR;C:public_html" |
1
2
3
4
5
6
7
8
9
10
|
$fruits = array ( 'apples' , 'oranges' , 'bananas' ); // "old" way // 学Javascript得数组了 $fruits = [ 'apples' , 'oranges' , 'bananas' ]; // 关联数组 $array = [ 'foo' => 'bar' , 'bar' => 'foo' ]; |
1
2
3
4
5
|
function foobar() { return [ 'foo' => [ 'bar' => 'Hello' ]]; } echo foobar()[ 'foo' ][ 'bar' ]; // Hello |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class Foo { function hello() { echo 'Hello Cichui!' ; } function anonymous() { return function () { $this ->hello(); // 之前是不可能这么玩得 }; } } 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! |
1
2
3
4
5
6
7
|
function anonymous() { $that = $this ; // $that is now $this return function () use ( $that ) { $that ->hello(); }; } |
1
2
3
4
|
echo 0b11111; // PHP 5.4支持二进制了 echo 31; // 十进制 echo 0x1f; // 十六进制 echo 037; // 八进制 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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!' ;}); // 闭包函数 echo my_function( 'my_callback_function' ); // 回调函数 echo my_function([ 'Hello' , 'hi' ]); // 类名,静态方法 echo my_function([( new Hi), 'hello' ]); // 类名,方法名 |
1
|
echo 'Executed in ' , round (microtime(true) - $_SERVER [ 'REQUEST_TIME_FLOAT' ], 2) |