代码之家  ›  专栏  ›  技术社区  ›  streetparade

如何在php静态函数中返回当前对象

  •  2
  • streetparade  · 技术社区  · 15 年前

    我需要在静态方法中访问当前对象。

    代码:

    protected static $name;
    
    public static function name($modulename)
    {
        self::$name = $modulename;
    }
    
    public function __call($name, $arguments)
    {
    
        $me = new test(self::$name);
        return $me->$name($arguments);
    }
    

    我希望能够在日志类中调用方法日志。这样地

    echo Mods::name("Log")->log("test") ;

    1 回复  |  直到 12 年前
        1
  •  3
  •   webbiedave    15 年前

    听起来你想要一个单一模式:

    http://www.talkphp.com/advanced-php-programming/1304-how-use-singleton-design-pattern.html

    使用log作为单例,并具有mods::name call log::getInstance();