Что является эквивалентом self в классах Python в классах PHP? - PullRequest
2 голосов
/ 17 августа 2011

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

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

    def otherfunc(self):
        print self.variable

Ответы [ 2 ]

8 голосов
/ 17 августа 2011

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

2 голосов
/ 17 августа 2011

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

...