如何在 PHP 中返回类变量作为引用(&/& 符号)?

发布于 2024-09-17 09:50:42 字数 356 浏览 9 评论 0原文

这是我的代码示例。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2024-09-24 09:50:42

它是:

function &getConnection()
{
   return $this->conn;
}

并且在调用时:

$conn = &$instance->getConnection();

这就是如何通过引用返回变量 一般来说,但对于资源类型 你不需要的变量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文