盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

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

时间:2024/11/28作者:未知来源:盾怪网教程人气:

[摘要]b;?> 以前代码中的用户自定义类或方法中虽未定义"const”关键字,但无需编辑即可运行。例外PHP 4 had no exception handling. PHP 5 intr...
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);
?>



关键词:PHP 5昨天隆重推出--PHP 5/Zend Engine 2.0新特征




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版