@
@
@atambalasi2015-12-13 20:24:32
PHP
@atambalasi, 2015-12-13 20:24:32

How to call non-static methods or properties from a static method?

for example

class Dream
{
     private $mydream;
     private $findDream;
     public static function whichDream()
     {
         //Here
     }
     public function findDream()
    {
       // SOmething......
    }

}

How to call the usual findDream method from the wichDream static method??

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AnjeyTsibylskij, 2015-12-13
@AnjeyTsibylskij

As an option

class Dream
{
    private $mydream;
    private $findDream;
  
  private static $self = null;
  
  public static function _self()
  {
    if(is_null(self::$self)) {
      self::$self = new self;
    }
    
    return self::$self;
  }
  
    public static function whichDream()
    {
    return self::_self()->findDream();
    }
    
  public function findDream()
    {
       echo 'Test';
    }
}

Dream::whichDream();

A
Alexander Aksentiev, 2015-12-13
@Sanasol

self::findDream()

M
Miku Hatsune, 2015-12-13
@Hatsune-Miku

static::findDream()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question