在 __construct() 中回显
如何读取 __construct() 中的变量?
这是示例代码:
class Sample {
private $test;
public function __construct(){
$this->test = "Some text here.";
}
}
$sample = new Sample();
echo $sample->test;
这段代码有什么问题?因为 __construct 是自动的,所以我只是认为它将在类示例上运行并自动读取它。
是否可以在不接触 __construct() 的情况下回显这一点? 谢谢。
How to read variable inside __construct()?
Here's the sample code:
class Sample {
private $test;
public function __construct(){
$this->test = "Some text here.";
}
}
$sample = new Sample();
echo $sample->test;
What is wrong with this code? Because __construct is automatic, I just thought that it will run on class sample and read it automatically.
Is it possible to echo this out without touching __construct()?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
$test
设为公开。当它是私有的时,它只能从类内部读取。You need to make
$test
public. When it's private, it is only readable from within the class.