[英]How to return a class variable as a reference (&/ampersand) in PHP?
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. 我的主要目的是我想通过引用它来返回MySQLi连接,而不是创建另一个MySQLi类。
It's: 它的:
function &getConnection()
{
return $this->conn;
}
and when calling: 并在致电时:
$conn = &$instance->getConnection();
That's how to return variables by reference in general, but for resource
type variables you don't need to. 通常,这就是通过引用返回变量的方法 ,但是对于resource
类型变量,则不需要这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.