在php中获取设置属性

新手上路,请多包涵

我来自 C# 环境,我开始在学校学习 PHP。我习惯于像这样在 C# 中设置我的属性。

 public int ID { get; set; }

php中的this等价于什么?

谢谢。

原文由 Jeppe Strøm 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 267
1 个回答

`php

类 MyClass { 私人 $name = null;

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

public function __set($name, $value)
{
    if (property_exists($this, $name)) {
        $this->name = $value;
    }
    return $this;
}

public function __get($name)
{
    if (property_exists($this, $name)) {
        return $this->$name;
    }
    return null;
}

}

`

原文由 George John 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题