<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        PHP 5昨天隆重推出--PHP 5/Zend Engine 2.0新特性

        來源:懂視網 責編:小采 時間:2020-11-27 19:01:15
        文檔

        PHP 5昨天隆重推出--PHP 5/Zend Engine 2.0新特性

        PHP 5昨天隆重推出--PHP 5/Zend Engine 2.0新特性: 前言 今天突然想到PHP官方網站上一轉,一眼就看到PHP5推出的通告。雖然以前看到過PHP5的預告,但還是仔細看了PHP 5/Zend Engine 2.0新特性一文,一股JAVA氣息撲面而來... 特將該文試譯出來,首發于CSDN網站,以饗讀者。PHP 5/Zend Engine
        推薦度:
        導讀PHP 5昨天隆重推出--PHP 5/Zend Engine 2.0新特性: 前言 今天突然想到PHP官方網站上一轉,一眼就看到PHP5推出的通告。雖然以前看到過PHP5的預告,但還是仔細看了PHP 5/Zend Engine 2.0新特性一文,一股JAVA氣息撲面而來... 特將該文試譯出來,首發于CSDN網站,以饗讀者。PHP 5/Zend Engine


        前言

           今天突然想到PHP官方網站上一轉,一眼就看到PHP5推出的通告。雖然以前看到過PHP5的預告,但還是仔細看了PHP 5/Zend Engine 2.0新特性一文,一股JAVA氣息撲面而來...
           特將該文試譯出來,首發于CSDN網站,以饗讀者。

        PHP 5/Zend Engine 2.0新特性
        徐喚春 譯 sfwebsite@hotmail.com
        http://www.php.com/zend-engine-2.php

        全新的對象模型
        PHP中的對象處理部分已完全重寫,具有更佳的性能和更多的功能。在PHP的以前版本中,對象與內建變量類型(如integer和string)的處理方法相同,其弊端是當變量被賦值為對象或對象作為參數傳遞時,得到的是對象復制品。而在新版本中,對象通過句柄進行引用,而不是通過它的值。(句柄可以認是為是對象的標識符)
        很多PHP程序員可能未意識到以前的對象模型的“復制怪癖”,因此以前的PHP程序將不需要做任何更改,或只做很小的改動即可運行
        私有和保護成員
        PHP 5引入了私有和保護成員變量,它們可以定義類屬性在何時可以被訪問。

        類的保護成員變量能在該類的擴展類中被訪問,而私有成員變量只能在本類中被訪問。
        <?php
        class MyClass {
            private $Hello = "Hello, World!\n";
            protected $Bar = "Hello, Foo!\n";
            protected $Foo = "Hello, Bar!\n";

            function printHello() {
                print "MyClass::printHello() " . $this->Hello;
                print "MyClass::printHello() " . $this->Bar;
                print "MyClass::printHello() " . $this->Foo;
            }
        }

        class MyClass2 extends MyClass {
            protected $Foo;

            function printHello() {
                MyClass::printHello();                          /* Should print */
                print "MyClass2::printHello() " . $this->Hello; /* Shouldn't print out anything */
                print "MyClass2::printHello() " . $this->Bar;   /* Shouldn't print (not declared)*/
                print "MyClass2::printHello() " . $this->Foo;   /* Should print */
            }
        }

        $obj = new MyClass();
        print $obj->Hello;  /* Shouldn't print out anything */
        print $obj->Bar;    /* Shouldn't print out anything */
        print $obj->Foo;    /* Shouldn't print out anything */
        $obj->printHello(); /* Should print */

        $obj = new MyClass2();
        print $obj->Hello;  /* Shouldn't print out anything */
        print $obj->Bar;    /* Shouldn't print out anything */
        print $obj->Foo;    /* Shouldn't print out anything */
        $obj->printHello();
        ?>
        私有和保護方法
        在PHP 5(ZEND引擎2)中,還引入了私有和保護方法。
        例:
        <?php
        class Foo {
            private function aPrivateMethod() {
                echo "Foo::aPrivateMethod() called.\n";
            }

            protected function aProtectedMethod() {
                echo "Foo::aProtectedMethod() called.\n";
                $this->aPrivateMethod();
            }
        }

        class Bar extends Foo {
            public function aPublicMethod() {
                echo "Bar::aPublicMethod() called.\n";
                $this->aProtectedMethod();
            }
        }

        $o = new Bar;
        $o->aPublicMethod();
        ?>
        以前代碼中的用戶自定義類或方法中雖未定義"public," "protected" 或 "private"等關鍵字,但無需編輯即可運行。
        抽象類和方法
        PHP 5還引入了抽象類和方法。抽象方法只聲明方法定義, 不供實際運行。包含抽象方法的類需要聲明為抽象類。
        例:
        <?php
        abstract class AbstractClass {
            abstract public function test();
        }

        class ImplementedClass extends AbstractClass {
            public function test() {
                echo "ImplementedClass::test() called.\n";
            }
        }

        $o = new ImplementedClass;
        $o->test();
        ?>
        抽象類不能實例化。以前代碼中的用戶自定義類或方法中雖未定義"abstract”關鍵字,但無需編輯即可運行。
        接口
        ZEND引擎2.0引入了接口。一個類可以運行任意的接口列表。
        Example
        例:
        <?php
        interface Throwable {
            public function getMessage();
        }

        class Exception implements Throwable {
            public function getMessage() {
            // ...
        }
        ?>
        以前代碼中的用戶自定義類或方法中雖未定義"interface”關鍵字,但無需編輯即可運行。
        類類型定義
        在保留類無需定義類型的同時,PHP 5引入了類類型定義來聲明希望把哪個類通過參數傳遞給一個方法。
        Example
        例:
        <?php
        interface Foo {
            function a(Foo $foo);
        }

        interface Bar {
            function b(Bar $bar);
        }

        class FooBar implements Foo, Bar {
            function a(Foo $foo) {
                // ...
            }

            function b(Bar $bar) {
                // ...
            }
        }

        $a = new FooBar;
        $b = new FooBar;

        $a->a($b);
        $a->b($b);
        ?>
        這些類類型定義在不象一些需要類型預定義的語言在編譯中進行檢查,而是在運行時進行。這意味著:
        <?php
        function foo(ClassName $object) {
            // ...
        }
        ?>
        等價于:
        <?php
        function foo($object) {
            if (!($object instanceof ClassName)) {
                die("Argument 1 must be an instance of ClassName");
            }
        }
        ?>
        本語法只用于對象或類,不適用于內建類型。

        final
        PHP 5引入了“final”關鍵字定義在子類中不能被覆蓋的成員或方法。
        例:
        <?php
        class Foo {
            final function bar() {
                // ...
            }
        }
        ?>
        以前代碼中的用戶自定義類或方法中雖未定義"final"關鍵字,但無需編輯即可運行。
        對象克隆
        PHP 4在對象被復制時,用戶不能決定拷貝的機制。在復制時,PHP 4只一位一位地復制一個和原來對象一模一樣的復制品。
        我們并不是每次都要建立一個完全一樣的復制品。一個很好的需要一種復制機制的例子是,當有一個代表一個GTK窗口的對象,它擁有該窗口的所有資源,當你建立一個拷貝時,你可能需要一個新的窗口,它擁有原窗口的所有屬性,但需要擁有新窗口的資源。另外一個例子是你有一個對象引用了另外一個對象,當你復制父對象時,你希望建立那個引用對象的新實例,以使復制品引用它。
        對一個對象的拷貝通過調用對象的__clone()方法完成:
        <?php
        $copy_of_object = $object->__clone();
        ?>
        當開發者請求建立一個對象的新的拷貝時,ZEND引擎會檢查是否定義了__clone()方法。如果未定義的話,它會調用一個默認的__clone()方法來復制該對象的所有屬性。如果定義了該方法,該方法會負責在拷貝中設置必要的屬性。為方便起見,引擎會提供一個函數從源對象中導入所有的屬性,這樣它就可以先得到一個具有值的源對象拷貝,只需要對需要改變的屬性進行覆蓋即可。
        例:
        <?php
        class MyCloneable {
            static $id = 0;

            function MyCloneable() {
                $this->id = self::$id++;
            }

            function __clone() {
                $this->name = $that->name;
                $this->address = "New York";
                $this->id = self::$id++;
            }
        }

        $obj = new MyCloneable();

        $obj->name = "Hello";
        $obj->address = "Tel-Aviv";

        print $obj->id . "\n";

        $obj = $obj->__clone();

        print $obj->id . "\n";
        print $obj->name . "\n";
        print $obj->address . "\n";
        ?>
        統一的構造方法名
        ZEND引擎允許開發者定義類的構造方法。具有構造方法的類在新建時會首先調用構造方法,構造方法適用于在正式使用該類前進行的初始化。
        在PHP4中,構造方法的名稱與類名相同。由于在派生類中調用父類的作法比較普遍,因此導致在PHP4中當類在一個大型的類繼承中進行移動時,處理方式有點笨拙。當一個派生類被移動到一個不同的父類中時,父類的構造方法名必然是不同的,這樣的話派生類中的有關調用父類構造方法的語句需要改寫。
        PHP 5 introduces a standard way of declaring constructor methods by calling them by the name __construct().
        PHP5引入了方法名__construct()來定義構造方法。
        Example
        <?php
        class BaseClass {
            function __construct() {
                print "In BaseClass constructor\n";
            }
        }

        class SubClass extends BaseClass {
            function __construct() {
                parent::__construct();
                print "In SubClass constructor\n";
            }
        }

        $obj = new BaseClass();
        $obj = new SubClass();
        ?>
        為向下兼容,PHP5當在類不能找到__construct()方法時,會通過老的方法也就是類名來查找構造方法。這意味著唯一可能產生兼容性問題的是在以前的代碼中已經使用了一個名為__construct()的方法名。
        析構方法
        定義析構方法是十分有用的。析構方法可以記錄調試信息,關閉數據庫連接,還有做其它的掃尾工作。PHP4中并無此機制,盡管PHP已支持注冊在請求結束時需要運行的函數。
        PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as Java: When the last reference to an object is destroyed the object's destructor, which is a class method name %__destruct()% that recieves no parameters, is called before the object is freed from memory.
        PHP5引入了與其它面向對象語言如Java語言相似的析構方法:當最后一個該對象的引用被清除時,系統將會在該對象從內存中釋放前調用名為__destruct()的析構方法。
        例:
        <?php
        class MyDestructableClass {
            function __construct() {
                print "In constructor\n";
                $this->name = "MyDestructableClass";
            }

            function __destruct() {
                print "Destroying " . $this->name . "\n";
            }
        }

        $obj = new MyDestructableClass();
        ?>
        和構造方法相似,引擎將不調用父類的析構方法,為調用該方法,你需要在子類的析構方法中通過parent::__destruct()語句進行調用。
        常量
        PHP 5 引入了類常量定義:
        <?php
        class Foo {
            const constant = "constant";
        }

        echo "Foo::constant = " . Foo::constant . "\n";
        ?>

        PHP5允許常量中有表達式,但在編譯時常量中的表達式將被計算.,因此常量不能在運行中改變它的值。
        <?php
        class Bar {
            const a = 1<<0;
            const b = 1<<1;
            const c = a | b;
        }
        ?>
        以前代碼中的用戶自定義類或方法中雖未定義"const”關鍵字,但無需編輯即可運行。
        例外
        PHP 4 had no exception handling. PHP 5 introduces a exception model similar to that of other programming languages.
        PHP4中無例外處理,PHP5引用了與其它語言相似的例外處理模型。
        例:
        <?php
        class MyExceptionFoo extends Exception {
            function __construct($exception) {
                parent::__construct($exception);
            }
        }

        try {
            throw new MyExceptionFoo("Hello");
        } catch (MyException $exception) {
            print $exception->getMessage();
        }
        ?>
        以前代碼中的用戶自定義類或方法中雖未定義'catch', 'throw' 和 'try'關鍵字,但無需編輯即可運行。
        函數返回對象值
        In PHP 4 it wasn't possible to dereference objects returned by functions and make further method calls on those objects. With the advent of Zend Engine 2, the following is now possible:
        在PHP4中,函數不可能返回對象的值并對返回的對象進行方法調用,通過ZEND引擎2中,這一切變得可能:
        <?php
        class Circle {
            function draw() {
                print "Circle\n";
            }
        }

        class Square {
            function draw() {
                print "Square\n";
            }
        }

        function ShapeFactoryMethod($shape) {
            switch ($shape) {
                case "Circle":
                    return new Circle();
                case "Square":
                    return new Square();
            }
        }

        ShapeFactoryMethod("Circle")->draw();
        ShapeFactoryMethod("Square")->draw();
        ?>
        靜態類中的靜態成員變量現在可初始化
        Example
        <?php
        class foo {
            static $my_static = 5;
        }

        print foo::$my_static;
        ?>
        靜態方法
        PHP5引入了關鍵字'static'來定義一個靜態方法,這樣可以從對象外進行調用。
        例:
        <?php
        class Foo {
            public static function aStaticMethod() {
                // ...
            }
        }

        Foo::aStaticMethod();
        ?>
        虛擬變量$this在靜態方法中無效。
        instanceof
        PHP5引入了關鍵字instanceof來確定一個對象是否是某一個對象的實例,或某一個對象的派生,或使用了某一個接口。
        例:
        <?php
        class baseClass { }

        $a = new baseClass;

        if ($a instanceof basicClass) {
            echo "Hello World";
        }
        ?>
        靜態函數變量
        所有的靜態變量現在在編譯時進行處理,這允許開發者通過引用來指定靜態變量。這個變化提高了效率但意味著不可能對靜態變量進行間接引用。
        函數中通過按地址傳送方式的參數允許定義默認值
        例:
        <?php
        function my_function(&$var = null) {
            if ($var === null) {
                die("$var needs to have a value");
            }
        }
        ?>
        __autoload()
        在初始化一個未定義的類時,引擎將自動調用__autoload()攔截器函數。該類名將作為__autoload()攔截器函數唯一參數傳遞給它。
        例:
        <?php
        function __autoload($className) {
            include_once $className . ".php";
        }

        $object = new ClassName;
        ?>
        方法和屬性調用的重載
        通用 __call(), __get() 和 __set()方法可以進行方法和屬性調用的重載。

        例: __get() 和 __set()
        <?php
        class Setter {
            public $n;
            public $x = array("a" => 1, "b" => 2, "c" => 3);

            function __get($nm) {
                print "Getting [$nm]\n";

                if (isset($this->x[$nm])) {
                    $r = $this->x[$nm];
                    print "Returning: $r\n";
                    return $r;
                } else {
                    print "Nothing!\n";
                }
            }

            function __set($nm, $val) {
                print "Setting [$nm] to $val\n";

                if (isset($this->x[$nm])) {
                    $this->x[$nm] = $val;
                    print "OK!\n";
                } else {
                    print "Not OK!\n";
                }
            }
        }

        $foo = new Setter();
        $foo->n = 1;
        $foo->a = 100;
        $foo->a++;
        $foo->z++;
        var_dump($foo);
        ?>
        例: __call()
        <?php
        class Caller {
            var $x = array(1, 2, 3);

            function __call($m, $a) {
                print "Method $m called:\n";
                var_dump($a);
                return $this->x;
            }
        }

        $foo = new Caller();
        $a = $foo->test(1, "2", 3.4, true);
        var_dump($a);
        ?>

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        PHP 5昨天隆重推出--PHP 5/Zend Engine 2.0新特性

        PHP 5昨天隆重推出--PHP 5/Zend Engine 2.0新特性: 前言 今天突然想到PHP官方網站上一轉,一眼就看到PHP5推出的通告。雖然以前看到過PHP5的預告,但還是仔細看了PHP 5/Zend Engine 2.0新特性一文,一股JAVA氣息撲面而來... 特將該文試譯出來,首發于CSDN網站,以饗讀者。PHP 5/Zend Engine
        推薦度:
        標簽: php 腳本 engine
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产精品99精品久久免费| a级男女仿爱免费视频| 亚洲国产综合91精品麻豆| 国产一级淫片a免费播放口之| 免费国产黄网站在线观看| eeuss免费影院| 亚洲天堂中文资源| 久久久亚洲精品蜜桃臀| 国产无遮挡吃胸膜奶免费看视频| 亚洲精品免费在线| 久久美女网站免费| 亚洲精品日韩一区二区小说| 亚洲色成人网一二三区| 亚洲精品成人片在线播放| 中文字幕日韩亚洲| 亚洲av中文无码| 国产精品冒白浆免费视频| 欧美日韩国产免费一区二区三区| 99视频精品全部免费观看| a级成人毛片免费视频高清| 一二三区免费视频| 日本亚洲免费无线码 | 亚洲爽爽一区二区三区| 99爱视频99爱在线观看免费| 国产性生大片免费观看性| 一级毛片高清免费播放| 免费人成动漫在线播放r18| 久久亚洲精品无码av| 亚洲黄网站wwwwww| 久久青青草原亚洲av无码app| 国产色爽免费视频| 在线观看免费为成年视频| 四虎成人免费大片在线| 成年女人毛片免费播放视频m| 久久久久久久岛国免费播放| 国产午夜不卡AV免费| 免费一级毛片无毒不卡| 99国产精品视频免费观看| 18观看免费永久视频| 亚洲综合免费视频| 成人无遮挡毛片免费看|