第十五节--Zend引擎的发展 -- Classes and Objects in PHP5 [15],第十五节--Zend引擎的发展 -- Classes and Objects in PHP5 [15]
【 tulaoshi.com - PHP 】
/*
------------------------------------------------------------------------------- 
| = 本文为Haohappy读<<Core PHP Programming 
| = 中Classes and Objects一章的笔记 
| = 翻译为主 个人心得 
| = 为避免可能发生的不必要的麻烦请勿转载,谢谢 
| = 欢迎批评指正,希望和所有PHP爱好者共同进步! 
| = PHP5研究中心: http://blog.csdn.net/haohappy2004
------------------------------------------------------------------------------- 
*/
第十五节--Zend引擎的发展
本章的最后一节,Zeev讨论了Zend引擎带来的对象模型,特别提到它与PHP的前几个版本中的模型有什么不同.
当1997年夏天,我们开发出PHP3, 我们没有计划要使PHP具备面向对象的能力. 当时没有任何与类和对象有关的想法. PHP3是一个纯粹面向过程的语言. 但是,在1997.8.27的晚上PHP3 alpha版中增加了对类的支持. 增加一个新特性给PHP,当时仅需要极少的讨论,因为当时探索PHP的人太少. 于是从1997年八月起, PHP迈出了走向面向对象编程语言的第一步.
确实,这只是第一步. 因为在这个设计中只有极少的相关的想法,对于对象的支持不够强大. 这个版本中使用对象仅是访问数组的一个很酷的方法而已. 取代使用$foo[bar],你可以使用看起来更漂亮的$foo-bar. 面向对象方法的主要的优势是通过成员函数或方法来储存功能. 例子6.18中显示了一个典型的代码块. 但是它和例6.19中的做法其实并没有太大不同.
Listing 6.18 PHP 3 object-oriented programming PHP3中的面向对象编程
<?php    class Example    {        var $value = "some value";        function PrintValue()        {            print $this-value;        }    }    $obj = new Example();    $obj-PrintValue(); ? <?php    function PrintValue($arr)    {        print $arr["value"];    }    function CreateExample()    {        $arr["value"] = "some value";        $arr["PrintValue"] = "PrintValue";        return $arr;    }    $arr = CreateExample();    //Use PHP's indirect reference    $arr["PrintValue"]($arr); ? <?php $a = 5; //$b points to the same place in memory as $a $b与$a指向内存中同个地址 $b = &$a; //we're changing $b, since $a is pointing to 改变$b,指向的地址改变 //the same place - it changes too $a指向的地址也改变 $b = 7; //prints 7 输出7 print $a; ?
1    class MyFoo { 2        function MyFoo() 3        { 4            $this-me = &$this; 5            $this-value = 5; 6        } 7 8        function setValue($val) 9        { 10            $this-value = $val; 11        } 12 13        function getValue() 14        { 15            return $this-value; 16        } 17 18        function getValueFromMe() 19        { 20            return $this-me-value; 21        } 22    } 23 24        function CreateObject($class_type) 25        { 26            switch ($class_type) { 27                case "foo": 28                    $obj = new MyFoo(); 29
                        来源:http://www.tulaoshi.com/n/20160129/1489743.html
看过《第十五节--Zend引擎的发展 -- Classes and Objects in PHP5 [15]》的人还看了以下文章 更多>>