doujiufutaog59220 2014-08-29 10:49
浏览 328
已采纳

函数中的Php remove()使其更易于使用

I am creating a little system which will allow users to extend the system with their own classes.

Class Core {
static $confArray;
static $extendArray;
protected static $instance;
public function read($name)
{
    return self::$confArray[$name];
}
public function put($name, $value)
{
    self::$confArray[$name] = $value;
}
public function extend($function, $handler, $args=null){

    self::$extendArray[$function] = new $handler($args);
}
public function __call($method, $args){
    return self::$extendArray[$method];
}
public static function getInstance()
{
    if (!isset(self::$instance))
    {
        $object =__CLASS__;
        self::$instance= new $object;
    }
    return self::$instance;
}
}

With That, now a user can come and register a simple extension from such a class:

class WorkersTest{
function isWorking($who){
    echo "$who is working";
}
function isNotWorking($who){
    echo "$who is not working";
}
}

To call the function (isworking/ isNotWorking), a the programmer needs to register the test class through:

Core::getInstance->extend("worker","WorkersTest");

Then it can now be called through:

Core::getInstance->worker()->isWorking("George");

This is working perfectly. My question is how i can remove the () in the call (dont worry why) and have:

Core::getInstance->worker->isWorking("George");

Is it possible?

  • 写回答

2条回答 默认 最新

  • doulian4467 2014-08-29 10:53
    关注

    You can use the magic __get() method, just like __call():

    public function __get($name)
    {
        return $this->$name();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?