如何在 PHP 中返回类变量作为引用(&/& 符号)?
这是我的代码示例。
class SomeClass extends SomeOtherClass
{
function __construct($input)
{
parent::__construct($input);
$this->conn = new mysqli('a','b','c','d');
}
function getConnection()
{
return &$this->conn;
}
}
我的主要目标是我想通过引用 MySQLi 连接来返回它,而不是创建另一个 MySQLi 类。
This is sample of my code.
class SomeClass extends SomeOtherClass
{
function __construct($input)
{
parent::__construct($input);
$this->conn = new mysqli('a','b','c','d');
}
function getConnection()
{
return &$this->conn;
}
}
My main object is that i want to return the MySQLi connection by referencing it instead of creating another MySQLi class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是:
并且在调用时:
这就是如何通过引用返回变量 一般来说,但对于
资源
类型 你不需要的变量。It's:
and when calling:
That's how to return variables by reference in general, but for
resource
type variables you don't need to.