1)_callStatic() magic 方法
classFoo{ publicstaticfunction__callStatic( $name, $args) { echo"Called method $name statically"; } publicfunction__call( $name, $args) { echo"Called method $name"; }}
Foo::dog(); // outputs "Called method dog statically"$foo= newFoo;$foo->dog(); // outputs "Called method dog"
2)動態調用函數
classDog{ publicfunctionbark() { echo"Woof!"; }} $class= "Dog"$action= "bark";$x= new$class(); // instantiates the class "Dog"$x->$action(); // outputs "Woof!"
3) 標準PHP庫(SPL)
加了了少數幾個容器類,比如,棧(SplStack)和固定數組(SplFixedArray)
$stack= newSplStack(); // push a few new items on the stack$stack->push("a");$stack->push("b");$stack->push("c"); // see how many items are on the stackechocount($stack); // returns 3 // iterate over the items in the stackforeach( $stackas$item) echo"[$item],";// the above outputs: 1 [/c],[b],[a] // pop an item off the stack echo $stack->pop(); // returns "c" // now see how many items are on the stack echo count($stack); // returns 2
4) Closures 功能
關於Closures,這是一個把函數定義成變數的玩意。讓我們看幾個例子:
樣本一:
$string= "Hello World!";$closure= function() use($string) { echo$string; }; $closure();
Output:
Hello World!
樣本二 使用引用的變數
$x= 1$closure= function() use(&$x) { ++$x; } echo$x. "\\n";$closure();echo$x. "\\n";$closure();echo$x. "\\n";
Output:
1
2
3
樣本三,傳回值
functiongetAppender($baseString){ returnfunction($appendString) use($baseString) { return$baseString.$appendString; };}
樣本四,Reflection
classCounter{ private$x; publicfunction__construct() { $this->x = 0; } publicfunctionincrement() { $this->x++; } publicfunctioncurrentValue() { echo$this->x . "\\n"; }}$class= newReflectionClass("Counter");$method= $class->getMethod("currentValue");$closure= $method->getClosure()$closure();$class->increment();$closure();
Output:
0
1
樣本五,Reflection API
$closure= function($x, $y= 1) {};$m= newReflectionMethod($closure);Reflection::export ($m);Output:Method [ publicmethod __invoke ] { - Parameters [2] { Parameter #0 [ $x] Parameter #1 [ $y] }}
樣本六,Uses Case
$logdb= function($string) { Logger::log("debug","database",$string);};$db= mysqli_connect("server","user","pass");$logdb("Connected to database");$db->query("insert into parts (part, description) values ("Hammer","Pounds nails");$logdb("Insert Hammer into to parts table");$db->query("insert into parts (part, description) values ("Drill","Puts holes in wood");$logdb("Insert Drill into to parts table");$db->query("insert into parts (part, description) values ("Saw","Cuts wood");$logdb("Insert Saw into to parts table");
更為詳細的文章,請參考這裡,連結。
5) 使用namespace
新版的PHP會開始支援C++式的namespace,請參看樣本:
樣本一
/* Foo.php */<?phpnamespaceFoo;functionbar(){ echo"calling bar....";}?> /* File1.php */<?phpinclude"./Foo.php";Foo/bar(); // outputs "calling bar....";?> /* File2.php */<?phpinclude"./Foo.php";useFoo asns;ns/bar(); // outputs "calling bar....";?> /* File3.php */<?phpinclude"./Foo.php";useFoo;bar(); // outputs "calling bar....";?>
樣本二,多重namespace
<?phpnamespaceFoo;classTest {} namespaceBar;classTest {} $a= newFoo\\Test;$b= newBar\\Test; var_dump($a, $b); Output:object(Foo\\Test)#1 (0) {}object(Bar\\Test)#2 (0) {}Output:object(Foo\\Test)#1 (0) { }object(Bar\\Test)#2 (0) { }
樣本三,不同檔案中的namespace
/*定義*//* global.php */<?phpfunctionhello(){ echo"hello from the global scope!";}?> /* Foo.php */<?phpnamespaceFoo;functionhello(){ echo"hello from the Foo namespace!";}?> /* Foo_Bar.php */<?phpnamespaceFoo/Bar;functionhello(){ echo"hello from the Foo/Bar namespace!";}?> /*使用 */<?phpinclude"./global.php";include"./Foo.php";include"./Foo_Bar.php"; useFoo; hello(); // outputs "hello from the Foo namespace!"Bar\\hello(); // outputs "hello from the Foo/Bar namespace!"\\hello(); // outputs "hello from the global scope!"?>
更為詳細的文章,請參考這裡,連結。
6)開始支援Achieve包
正像JAR一樣,PHP也要開始支援自己的Achieve包了,叫作,Phar。PHP提供了一整套函數來協助開發人員建立和使用Phar,正如下面的樣本所示:
建立:
$p= newPhar("/path/to/my.phar", CURRENT_AS_FILEINFO | KEY_AS_FILENAME, "my.phar");$p->startBuffering();
建立檔案存根(stub)
$p->setStub("");
加入檔案:
$p["file.txt"] = "This is a text file";$p["index.php"] = file_get_contents("index.php");$p["big.txt"] = "This is a big text file";$p["big.txt"]->setCompressedBZIP2();//加入某目錄下所有的檔案$p->buildFromDirectory("/path/to/files","./\\.php$/");
使用Phar
include"myphar.phar";include"phar://myphar.phar/file.php";
更為詳細的文章,請參考這裡,連結。