I
I
Imran Guseynov2014-05-12 17:44:11
PHP
Imran Guseynov, 2014-05-12 17:44:11

How to solve problem with oop setter?

class sys{
  public $log = "<br />";

    static function log($newval)
    {$this->log.=$newval."<br />";}

    static function getlog()
    { return $this->log . "<br />";}
}

sys::log("установка");
sys::getlog();

I don't understand why it doesn't work.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Korshunov, 2014-05-12
@imran_stxa

if you want to use static, replace
with
self::$log

V
Viktor Vsk, 2014-05-12
@viktorvsk

As far as I understand, static functions don't instantiate an object, so they can't have $this
I could be very wrong
Ideas should be

class sys{
    public $log = "<br />";

    public function log($newval)
    {$this->log.=$newval."<br />";}

    public function getlog()
    { return $this->log . "<br />";}
}
$sys = new sys;

$sys->log("установка");
$sys->getlog();
var_dump($sys);

A
Alexander Kubintsev, 2014-05-12
@akubintsev

In principle, the answer has already been given above, but if you do not want to reinvent the wheel by creating a logger, then look at Monolog. It is considered the most popular logging library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question