晚期静态绑定问题

时间:2017-01-17 00:50:48

标签: php late-static-binding

我在自己的项目中遇到了一些麻烦。我编写了一个简单的考试系统,为了方便起见,我使用了Late Static Bindings。我有下一个代码:

<?php
class Controler {
    public $dir;
    public $name;
    public $file;

    public function __construct( $dir = ST_CONTROL_DIR ) {
        $this->dir = $dir;
    }

    public function __call( $name, $arguments = array() ) {
        $this->name = &$name;
        $this->file = $this->dir . $this->name . '.php';

        var_dump(get_called_class());

        $result = $this->controler_include( ... $arguments );
        return $result;
    }
    public static function __callStatic( $name, $arguments = array() ) {
        var_dump(get_called_class());
        $obj = new static();
        return $obj->$name( ... $arguments );
    }
    private final function controler_include(){
        return include $this->file;
    }
}

class Router extends Controler {
    public function __construct(){
        parent::__construct(ST_ROUTER_DIR);
    }
}


class Routing {
    protected $action;
    protected $params;
    protected $reffer;

    static public function initialize(){
        global $ROUTER;
        $ROUTER = new self;
    }
    static public function process(){
        global $ROUTER;
        xdebug_start_trace( ST_DIR.'trace' );
        if($ROUTER->action == 'api'){
            $ROUTER->action = array_shift( $ROUTER->params );
            trig( 'router_process_api_before' );
            return Api::{ $ROUTER->action }( ... $ROUTER->params );
        } else {
            trig( 'router_process_before' );
            return Router::{$ROUTER->action}( ... $ROUTER->params );
        }
        xdebug_stop_trace();
    }
}

我的方法用分隔文件编写。此外,类Router是Controler的子类,并且仅由包含其文件方法的文件夹不同。

我执行GET-query / login,类路由使用以下代码执行构建此页面:

<?php
reg( 'view_header_css', function(){
    ?><link href="<?php echo ST_CSS_PATH ?>login.css" rel="stylesheet"><?php
});

Controler::{'message'}();

我想在这里调用控制器类静态方法。我的结果:

string 'Router' (length=6)
string 'Router' (length=6)

XDebug追踪:

TRACE START [2017-01-17 00:19:00]
    0.0292     414792         -> trig('router_process_before') /public_html/vendor/core/Routing.php:40
    0.0293     414888           -> func_get_args() /public_html/vendor/core/Core.php:87
                                 >=> array (0 => 'router_process_before')
    0.0293     414936           -> Core->trig($name = 'router_process_before') /public_html/vendor/core/Core.php:87
    0.0293     414984             -> func_get_args() /public_html/vendor/core/Core.php:58
                                   >=> array (0 => 'router_process_before')
    0.0293     415288             -> array_shift(array (0 => 'router_process_before')) /public_html/vendor/core/Core.php:59
                                   >=> 'router_process_before'
    0.0294     415200             -> func_num_args() /public_html/vendor/core/Core.php:61
                                   >=> 1
                                 >=> NULL
                               >=> NULL
    0.0294     415008         -> Router::login() /public_html/vendor/core/Routing.php:41
    0.0295     415192           -> Controler::__callStatic($name = 'login', $arguments = array ()) /public_html/vendor/core/Routing.php:41
    0.0295     415376             -> Router->__construct() /public_html/vendor/core/Controler.php:44
    0.0295     415544               -> Controler->__construct($dir = '/public_html/vendor/router/') /public_html/vendor/core/Routing.php:77
                                     >=> NULL
                                   >=> NULL
    0.0295     415712             -> Router->login() /public_html/vendor/core/Controler.php:45
    0.0296     416072               -> Controler->__call($name = 'login', $arguments = array ()) /public_html/vendor/core/Controler.php:45
    0.0296     416344                 -> get_called_class() /public_html/vendor/core/Controler.php:25
                                       >=> 'Router'
    0.0296     416424                 -> var_dump('Router') /public_html/vendor/core/Controler.php:25
                                       >=> NULL
    0.0297     416344                 -> file_exists('/public_html/vendor/router/login.php') /public_html/vendor/core/Controler.php:33
                                       >=> TRUE
    0.0297     416328                 -> Controler->controler_include() /public_html/vendor/core/Controler.php:38
    0.0299     420376                   -> include(/public_html/vendor/router/login.php) /public_html/vendor/core/Controler.php:48
    0.0300     420784                     -> reg('view_header_css', class Closure {  }) /public_html/vendor/router/login.php:4
    0.0300     420968                       -> func_get_args() /public_html/vendor/core/Core.php:86
                                             >=> array (0 => 'view_header_css', 1 => class Closure {  })
    0.0300     420920                       -> Core->reg($name = 'view_header_css', $callback = class Closure {  }, $order = ???) /public_html/vendor/core/Core.php:86
    0.0301     421016                         -> is_null(NULL) /public_html/vendor/core/Core.php:45
                                               >=> TRUE
                                             >=> NULL
                                           >=> NULL
    0.0301     421384                     -> Controler->message() /public_html/vendor/router/login.php:7
    0.0302     421568                       -> Controler->__call($name = 'message', $arguments = array ()) /public_html/vendor/router/login.php:7
    0.0302     421696                         -> get_called_class() /public_html/vendor/core/Controler.php:25
                                               >=> 'Router'
    0.0302     421776                         -> var_dump('Router') /public_html/vendor/core/Controler.php:25
                                               >=> NULL
    0.0302     421696                         -> file_exists('/public_html/vendor/router/message.php') /public_html/vendor/core/Controler.php:33
                                               >=> FALSE
    0.0304     438336                         -> EControlerNotFound->__construct($object = class Router { public $dir = '/public_html/vendor/router/'; public $name = 'message'; public $file = '/public_html/vendor/router/message.php' }, $name = 'message', $file = '/public_html/vendor/router/message.php') /public_html/vendor/core/Controler.php:35
    0.0308      19352
TRACE END   [2017-01-17 00:19:00]

如果我打电话给View :: {&#39;消息&#39;} - 所有&#39;是正确的。 (View是Controler的孩子)

现在我的问题:当我调用父类的静态方法时,有人可以解释为什么PHP执行子类的非静态方法吗?

0 个答案:

没有答案