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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章