简体   繁体   中英

Calling a non static function in a static child class php

I have a singleton class that extends a normal class. (Class1)

The normal class has two non-static functions called set() and get(). (Class2)

A third class (Class3) which gets the singleton's (Class1's) instance uses the set() and get() through the singleton. It works fine using Class3, but is there any way to use the get() method inside the singleton from the parent class to see what the "third" class set it to?

I can't seem to call the get because it's non static. Please let me know if this is confusing.

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 }
}

I've never seen specific class just for a singleton which extends the class it is supposed to be a singleton for. Instead, try this:

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');

I just solved my own question, sorry for the late reply.

All I had to do in the singleton was call get() this way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM