php - 在 PHP 类中,python 类中的 self 等价于什么?

标签 php python class self

在 PHP 中,我怎样才能从 Python 实现类似的东西?

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

    def otherfunc(self):
        print self.variable

最佳答案

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

关于php - 在 PHP 类中,python 类中的 self 等价于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7084564/

相关文章:

javascript - 黑客如何将 javascript 代码注入(inject)我的 homepage.php

php - mySQL不会创建表

python - Pandas 数据框 - 选择行并清除内存?

php - MySQL 连接 : globally or in object?

javascript - 使用 JavaScript 向 img 标签添加类

Java - "Cannot find symbol"错误

php - Codeigniter form_validation 总是返回 false

php - 为什么我不需要使用 htmlEntities?

python - 收藏速度更快,类似pandas系列的计数器操作

javascript - MongoDB聚合管道: use of For loop