【发布时间】:2014-08-16 05:14:57
【问题描述】:
在 PHP 中访问私有/受保护变量的正确方法是什么?
我了解到可以使用__construct 函数访问它。
例如。
class helloWorld
{
public $fname;
private $lname;
protected $full;
function __construct()
{
$this->fname = "Hello";
$this->lname = "World";
$this->full = $this->fname . " " . $this->lname;
}
}
或者创建一个Getter 或Setters 函数。我不知道这是否正确。
class helloWorld
{
public $fname;
private $lname;
protected $full;
function getFull(){
return $this->full;
}
function setFull($fullname){
$this->full = $fullname;
}
}
或通过__toString。我很困惑我应该使用什么。抱歉,我还是 OOP 的新手。还有什么是 php 中的:: 符号,我该如何使用它?
谢谢:)
【问题讨论】:
-
从here开始
-
取决于您可能想要访问所述属性的用例