PHP Get Returned Value From Constructor(PHP 从构造函数获取返回值)
本文介绍了PHP 从构造函数获取返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很困惑,我有一个带有构造函数的 PHP 类:
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();
我希望 $t 的内容是一些文本":
I would expect the contents of $t to be "some text":
print_r($t);
但它是:
Test Object
(
)
如何从构造函数中获取返回值"some text"
?
How do I get the returned value "some text"
from the constructor?
推荐答案
你不能从构造函数return
任何东西.new
关键字总是会产生一个新的对象,不管你从构造函数中return
什么.
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 从构造函数获取返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:PHP 从构造函数获取返回值


基础教程推荐
猜你喜欢
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01