在静态子类php中调用非静态函数


Calling a non static function in a static child class php

我有一个扩展普通类的singleton类。(类别1)

普通类有两个非静态函数,分别称为set()和get()。(2类)

第三个类(Class3)获取singleton(Class1)的实例,通过singleton使用set()和get()。使用Class3可以很好地工作,但是有没有办法在父类的singleton中使用get()方法来查看"第三"类将其设置为什么?

我似乎无法调用get,因为它是非静态的。如果这令人困惑,请告诉我。

class Class1 {
     public function doThings(){
         $this->view->set("css","1234");
     }
}
class Singleton extends Class3 {
      static public function instance()
      {
           if (!self::$_instance instanceof self) {
           self::$_instance = new self();
           }
          return self::$_instance;
      }
      //I want this singleton to call get("css") and have it return the value.
}

class Class3{
     public function get(arg){//implementation } 
     public function set(arg){//implementation }
}

我从未见过专门针对singleton的特定类,它扩展了它应该是singleton的类。相反,试试这个:

class Class3
{  
    private $_instance;
    static public function instance()
    {
        if (!self::$_instance instanceof self) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    public function get(arg){//implementation } 
    public function set(arg){//implementation }
}
// Calling code
// I want this singleton to call get("css") and have it return the value.
Class3::getInstance()->set('css', 'border:0');
Class3::getInstance()->get('css');

我刚刚解决了自己的问题,很抱歉回复太晚。

我在singleton中所要做的就是这样调用get():

self::instance()->get("css");