PHP5.3 新特性 之 静态调用August 12, 2008

Late Static Bindings

考虑下述代码的输出

<?php
class clsParent {
    static public function say( $str ) {
        self::do_print($str);
    }

    static public function do_print( $str ) {
        echo "parent says $str";
    }
}

class clsChild extends clsParent{
    static public function do_print( $str ) {
        echo "child says $str";
    }
}

clsChild::say('Hello');
?>

你可能很希望输出 'child says Hello',但是实际上输出的是 'parent says Hello'。这是因为 self:: 指向的是类的本身,也就是 clsParent(同道理,__CLASS__ 也是一样)。

针对这一情况,PHP5.3 [引入了

§ 4 条评论

  1. x x

    你这php写的,越来越像java了.
    尤其是 static public function say( $str ) {

  2. @x -_-! PHP 的确从 5 以后 OOP 方面有点向 Java 靠拢

  3. 脑袋不够用 oop一直让人理解不了粉晕粉晕

  4. 静态的类或方法也要继承?

添加评论




* Required (but your email address will never be published)

Yahoo 统计