В классах PHP, что эквивалентно self из классов python?


В PHP, как я могу добиться чего-то подобного с помощью Python?

class CrowdProcess():
    def __init__(self,variable):
        self.variable = variable

    def otherfunc(self):
        print self.variable
Author: João Pinto Jerónimo, 2011-08-17

2 answers

PHP использует $this в качестве ссылки на экземпляр:

class CrowdProcess
{
    public $variable;

    public function __construct($variable)
    {
        $this->variable = $variable;
    }

    public function otherfunc()
    {
        echo $this->variable, PHP_EOL;
    }
}

Для получения дополнительной информации см. http://php.net/language.oop5.basic#example-155

 8
Author: , 2011-08-16 20:41:50

Вы можете использовать $this->variable в PHP:)

 2
Author: Cydonia7, 2011-08-16 20:30:48