开发者

How to return a class variable as a reference (&/ampersand) in PHP?

开发者 https://www.devze.com 2023-01-15 01:25 出处:网络
This is sample of my code. class SomeClass extends SomeOtherClass { function __construct($input) { parent::__construct($input);

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消