PHP从构造函数返回值 [英] PHP Get Returned Value From Constructor
本文介绍了PHP从构造函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很困惑,我有一个PHP类与一个构造函数:
class Test {
public function __construct (){
returnsome text;
}
}
然后我建立一个对象:
$ t = new Test();
我期望$ t的内容是一些文本:
print_r($ t);
但它是:
测试对象
(
)
我从构造函数中获得返回值一些文本
解决方案
你不能从构造函数中返回
任何东西。 new
关键字将总是产生一个新对象,从构造函数中返回
p>
I am confused, I have a PHP class with a constructor:
class Test {
public function __construct() {
return "some text";
}
}
Then I instanciate an object:
$t = new Test();
I would expect the contents of $t to be "some text":
print_r($t);
But it is:
Test Object
(
)
How do I get the returned value "some text"
from the constructor?
解决方案
You cannot return
anything from a constructor. The new
keyword will always result in a new object, it does not matter what you return
from the constructor.
这篇关于PHP从构造函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文