zend framework2 相對於zend framework1 的改進

    zend framework(一下簡稱zf)作爲zend公司的一個重要產品是php的開源框架,基於mvc模式,是php圈內主流開發框架之一,2012年9月5日zend公司正式發佈了zf2,

下面一起來圍觀一下zf2相對於zf1都做了哪幾方面的改進。

    首先對比一下二者的官方介紹文檔:

zf1:Zend Framework is an open source framework for developing web applications and services with PHP 5. Zend Framework is implemented using 100%

object-oriented code. The component structure of Zend Framework is somewhat unique; each component is designed with few dependencies on other components.

This loosely coupled architecture allows developers to use components individually. We often call this a "use-at-will" design.

zf2:Zend Framework 2 is an open source framework for developing web applications and services using PHP 5.3+. ZendFramework 2 uses 100% object-orientedcode

and utilises most of the new features of PHP 5.3, namely namespaces, late static binding, lambda functions and closures.

我們可以發現在zf2中出現了幾個新的詞彙PHP 5.3+,namespaces,late static binding,Lambda functions and closures.這就是zf2最大的改進,當然這也是必然的因爲
php5.3相對於5.2也做了很多改變,zf2的發佈正是爲了支持這些功能,下面我們來逐個學習。
其實討論zf2的改進與討論php5.3相對於php5.2的改進沒有什麼區別。
1:php5.3,現在運行phpinfo()看看你的php版本吧,如果還停留在php5.2的話,趕緊去php官網更新吧(http://www.php.net/);
2:namespaces:命名空間,php5.3自引進了命名空間後爲廣大phper(爲了方便自創的詞,php開發者)省去了很多麻煩,有效的解決了函數
或者類的命名問題。
3:late static binding:延遲靜態綁定,手冊中是這樣解釋的As of PHP 5.3.0, PHP implements a feature called late static bindings
which can be used to reference the called class in a context of static inheritance. 翻譯過來就是,php5.3可以在靜態繼承中引
用一個已被調用的類
看看手冊提供的示例
在php5.2中
<?php
    class A
    {
        public static function who()
        {
            echo __CLASS__;
        }
        public static function test()
        {
            self :: who();//注意5.2 還不支持static
        }
    }
    class B extends A
    {
        public static function who()
        {
            echo __CLASS__;
        }
    }
    B::test();
?>
這裏的輸出結果是A,說明self綁定的仍然是class A
在php5.3中

<?php
class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        static::who(); // 後期靜態綁定從這裏開始
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();
?>
這裏的輸出結果是B,這就是說static並沒有在class A就綁定,而是在調用function test()時
才綁定的,由於是在class B中調用的所以綁定的是 class B ,
4:Lambda funcitons:匿名函數,closures:閉包:匿名函數(Anonymous functions),也叫閉包函數(closures),
允許 臨時創建一個沒有指定名稱的函數。最經常用作回調函數(callback)的參數。 當然,也有其他應用的情況。
比如說php5.3開始支持這樣的用法了
<?php
    $fun = function ($name)
    {
        echo $name;
    }
    echo $fun("曹志攀");
?>
更詳細的用法請參見php manual;
總之,php5.3是php的又一大進步,php是一門年輕的語言,但隨着新版本的不斷髮布時刻緊跟潮流而又不落俗套
love php,love your future!!

發佈了45 篇原創文章 · 獲贊 2 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章