• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題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關(guān)鍵字專題關(guān)鍵字專題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
    當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

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

    來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 19:01:15
    文檔

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

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


    前言

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

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

    全新的對(duì)象模型
    PHP中的對(duì)象處理部分已完全重寫,具有更佳的性能和更多的功能。在PHP的以前版本中,對(duì)象與內(nèi)建變量類型(如integer和string)的處理方法相同,其弊端是當(dāng)變量被賦值為對(duì)象或?qū)ο笞鳛閰?shù)傳遞時(shí),得到的是對(duì)象復(fù)制品。而在新版本中,對(duì)象通過句柄進(jìn)行引用,而不是通過它的值。(句柄可以認(rèn)是為是對(duì)象的標(biāo)識(shí)符)
    很多PHP程序員可能未意識(shí)到以前的對(duì)象模型的“復(fù)制怪癖”,因此以前的PHP程序?qū)⒉恍枰鋈魏胃?,或只做很小的改?dòng)即可運(yùn)行
    私有和保護(hù)成員
    PHP 5引入了私有和保護(hù)成員變量,它們可以定義類屬性在何時(shí)可以被訪問。

    類的保護(hù)成員變量能在該類的擴(kuò)展類中被訪問,而私有成員變量只能在本類中被訪問。
    <?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();
    ?>
    私有和保護(hù)方法
    在PHP 5(ZEND引擎2)中,還引入了私有和保護(hù)方法。
    例:
    <?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"等關(guān)鍵字,但無需編輯即可運(yùn)行。
    抽象類和方法
    PHP 5還引入了抽象類和方法。抽象方法只聲明方法定義, 不供實(shí)際運(yùn)行。包含抽象方法的類需要聲明為抽象類。
    例:
    <?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();
    ?>
    抽象類不能實(shí)例化。以前代碼中的用戶自定義類或方法中雖未定義"abstract”關(guān)鍵字,但無需編輯即可運(yùn)行。
    接口
    ZEND引擎2.0引入了接口。一個(gè)類可以運(yùn)行任意的接口列表。
    Example
    例:
    <?php
    interface Throwable {
        public function getMessage();
    }

    class Exception implements Throwable {
        public function getMessage() {
        // ...
    }
    ?>
    以前代碼中的用戶自定義類或方法中雖未定義"interface”關(guān)鍵字,但無需編輯即可運(yùn)行。
    類類型定義
    在保留類無需定義類型的同時(shí),PHP 5引入了類類型定義來聲明希望把哪個(gè)類通過參數(shù)傳遞給一個(gè)方法。
    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);
    ?>
    這些類類型定義在不象一些需要類型預(yù)定義的語言在編譯中進(jìn)行檢查,而是在運(yùn)行時(shí)進(jìn)行。這意味著:
    <?php
    function foo(ClassName $object) {
        // ...
    }
    ?>
    等價(jià)于:
    <?php
    function foo($object) {
        if (!($object instanceof ClassName)) {
            die("Argument 1 must be an instance of ClassName");
        }
    }
    ?>
    本語法只用于對(duì)象或類,不適用于內(nèi)建類型。

    final
    PHP 5引入了“final”關(guān)鍵字定義在子類中不能被覆蓋的成員或方法。
    例:
    <?php
    class Foo {
        final function bar() {
            // ...
        }
    }
    ?>
    以前代碼中的用戶自定義類或方法中雖未定義"final"關(guān)鍵字,但無需編輯即可運(yùn)行。
    對(duì)象克隆
    PHP 4在對(duì)象被復(fù)制時(shí),用戶不能決定拷貝的機(jī)制。在復(fù)制時(shí),PHP 4只一位一位地復(fù)制一個(gè)和原來對(duì)象一模一樣的復(fù)制品。
    我們并不是每次都要建立一個(gè)完全一樣的復(fù)制品。一個(gè)很好的需要一種復(fù)制機(jī)制的例子是,當(dāng)有一個(gè)代表一個(gè)GTK窗口的對(duì)象,它擁有該窗口的所有資源,當(dāng)你建立一個(gè)拷貝時(shí),你可能需要一個(gè)新的窗口,它擁有原窗口的所有屬性,但需要擁有新窗口的資源。另外一個(gè)例子是你有一個(gè)對(duì)象引用了另外一個(gè)對(duì)象,當(dāng)你復(fù)制父對(duì)象時(shí),你希望建立那個(gè)引用對(duì)象的新實(shí)例,以使復(fù)制品引用它。
    對(duì)一個(gè)對(duì)象的拷貝通過調(diào)用對(duì)象的__clone()方法完成:
    <?php
    $copy_of_object = $object->__clone();
    ?>
    當(dāng)開發(fā)者請(qǐng)求建立一個(gè)對(duì)象的新的拷貝時(shí),ZEND引擎會(huì)檢查是否定義了__clone()方法。如果未定義的話,它會(huì)調(diào)用一個(gè)默認(rèn)的__clone()方法來復(fù)制該對(duì)象的所有屬性。如果定義了該方法,該方法會(huì)負(fù)責(zé)在拷貝中設(shè)置必要的屬性。為方便起見,引擎會(huì)提供一個(gè)函數(shù)從源對(duì)象中導(dǎo)入所有的屬性,這樣它就可以先得到一個(gè)具有值的源對(duì)象拷貝,只需要對(duì)需要改變的屬性進(jìn)行覆蓋即可。
    例:
    <?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";
    ?>
    統(tǒng)一的構(gòu)造方法名
    ZEND引擎允許開發(fā)者定義類的構(gòu)造方法。具有構(gòu)造方法的類在新建時(shí)會(huì)首先調(diào)用構(gòu)造方法,構(gòu)造方法適用于在正式使用該類前進(jìn)行的初始化。
    在PHP4中,構(gòu)造方法的名稱與類名相同。由于在派生類中調(diào)用父類的作法比較普遍,因此導(dǎo)致在PHP4中當(dāng)類在一個(gè)大型的類繼承中進(jìn)行移動(dòng)時(shí),處理方式有點(diǎn)笨拙。當(dāng)一個(gè)派生類被移動(dòng)到一個(gè)不同的父類中時(shí),父類的構(gòu)造方法名必然是不同的,這樣的話派生類中的有關(guān)調(diào)用父類構(gòu)造方法的語句需要改寫。
    PHP 5 introduces a standard way of declaring constructor methods by calling them by the name __construct().
    PHP5引入了方法名__construct()來定義構(gòu)造方法。
    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當(dāng)在類不能找到__construct()方法時(shí),會(huì)通過老的方法也就是類名來查找構(gòu)造方法。這意味著唯一可能產(chǎn)生兼容性問題的是在以前的代碼中已經(jīng)使用了一個(gè)名為__construct()的方法名。
    析構(gòu)方法
    定義析構(gòu)方法是十分有用的。析構(gòu)方法可以記錄調(diào)試信息,關(guān)閉數(shù)據(jù)庫連接,還有做其它的掃尾工作。PHP4中并無此機(jī)制,盡管PHP已支持注冊(cè)在請(qǐng)求結(jié)束時(shí)需要運(yùn)行的函數(shù)。
    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引入了與其它面向?qū)ο笳Z言如Java語言相似的析構(gòu)方法:當(dāng)最后一個(gè)該對(duì)象的引用被清除時(shí),系統(tǒng)將會(huì)在該對(duì)象從內(nèi)存中釋放前調(diào)用名為__destruct()的析構(gòu)方法。
    例:
    <?php
    class MyDestructableClass {
        function __construct() {
            print "In constructor\n";
            $this->name = "MyDestructableClass";
        }

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

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

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

    PHP5允許常量中有表達(dá)式,但在編譯時(shí)常量中的表達(dá)式將被計(jì)算.,因此常量不能在運(yùn)行中改變它的值。
    <?php
    class Bar {
        const a = 1<<0;
        const b = 1<<1;
        const c = a | b;
    }
    ?>
    以前代碼中的用戶自定義類或方法中雖未定義"const”關(guān)鍵字,但無需編輯即可運(yùn)行。
    例外
    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'關(guān)鍵字,但無需編輯即可運(yùn)行。
    函數(shù)返回對(duì)象值
    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中,函數(shù)不可能返回對(duì)象的值并對(duì)返回的對(duì)象進(jìn)行方法調(diào)用,通過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();
    ?>
    靜態(tài)類中的靜態(tài)成員變量現(xiàn)在可初始化
    Example
    <?php
    class foo {
        static $my_static = 5;
    }

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

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

    $a = new baseClass;

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

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

    例: __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);
    ?>

    聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。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官方網(wǎng)站上一轉(zhuǎn),一眼就看到PHP5推出的通告。雖然以前看到過PHP5的預(yù)告,但還是仔細(xì)看了PHP 5/Zend Engine 2.0新特性一文,一股JAVA氣息撲面而來... 特將該文試譯出來,首發(fā)于CSDN網(wǎng)站,以饗讀者。PHP 5/Zend Engine
    推薦度:
    標(biāo)簽: php 腳本 engine
    • 熱門焦點(diǎn)

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 午夜精品一区二区三区在线视| 93精91精品国产综合久久香蕉| 国产精品亚洲精品| 久久er热视频在这里精品| 国语自产精品视频在线观看| 中文精品无码中文字幕无码专区| 国产在线精品一区二区高清不卡 | 99久久综合国产精品二区| 亚洲国产精品国产自在在线| 女人高潮内射99精品| 亚洲国产精品自产在线播放| 久久97精品久久久久久久不卡| 91精品观看91久久久久久| 久久国产精品免费| 999成人精品视频在线| 亚洲国产精品不卡毛片a在线| 国产成人精品男人的天堂538| 91精品久久久久久无码| 无码人妻精品一区二区| 四虎永久在线精品884aa下载| 宅男在线国产精品无码| 人妻精品久久久久中文字幕69 | 国产成人精品久久亚洲高清不卡 | 国产91在线精品| 亚洲综合av永久无码精品一区二区| 精品免费视在线观看| 国产精品视频免费一区二区| 国产福利视精品永久免费| 亚洲处破女AV日韩精品| 精品国产自在在线在线观看| 精品久久久久中文字幕一区| 精品国产一区二区三区久久| 国产精品videossex白浆| 91精品国产福利尤物| 最新精品国偷自产在线| 成人国内精品久久久久影院| 亚洲精品无码永久在线观看你懂的| 91精品国产综合久久精品 | 国产中老年妇女精品| 亚洲av无码成人精品区| 国产微拍精品一区二区|