简体   繁体   English

尝试启动连接时为什么会出现NULL?

[英]Why am I getting NULL when I try to initiate a connection?

I have a class with global $connection but when I try to access it I am getting NULL. 我有一个带有全局$connection的类,但是当我尝试访问它时,我得到的是NULL。 As you can see if i try to access within the constructor I do not get NULL. 如您所见,如果我尝试在构造函数内访问,我不会得到NULL。 But from getConnection() I am getting NULL. 但是从getConnection()我得到NULL。

class DatabaseManipulation
{
    private $connection;

    function __construct()
    {
        global $connection;
        $connection = new mysqli("localhost", "root", "", "db");
        $result = $connection->query("select * from user");

        print_r($result); //I get an array
    }

    function getConnection(){
        global $connection;
        var_dump($connection); // I get -> object(DatabaseManipulation)#1 (1) { ["connection":"DatabaseManipulation":private]=> NULL } NULL 
    }

}

Same thing happens when I instantiate an object $connection = new DatabaseManipulation(); 当我实例化一个对象$connection = new DatabaseManipulation();时,也会发生同样的事情$connection = new DatabaseManipulation(); . Am I doing something wrong? 难道我做错了什么? I want this to be done in OO way. 我希望这可以通过OO方式完成。 Can anyone help me ? 谁能帮我 ?

You are using OO PHP not procedural. 您正在使用OO PHP而不是程序性的。 So change it to: 因此将其更改为:

$this->connection = new mysqli("localhost", "root", "", "db");;

I want this to be done in OO way. 我希望这可以通过OO方式完成。

Then stop using global . 然后停止使用global You don't want to refer to a global state when you're in a class. 在上课时,您不想引用全局状态。 A quick look to The Basics and you will find that a pseudo-variable $this is available inside your object. 快速浏览The Basics ,您会发现在对象内部有一个伪变量$this

class DatabaseManipulation
{
    private $connection;

    function __construct()
    {
        $this->connection = new mysqli("localhost", "root", "", "db");
    }

    function getConnection(){
        var_dump($this->connection);
    }
}

More about it: 关于它的更多信息:

  1. Why is Global State so Evil? 为什么全球国家如此邪恶?
  2. What does the variable $this mean in PHP? 变量$ this在PHP中是什么意思?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我尝试删除帖子时出现此错误? - Why am i getting this error when i try to delete a post? PHP对象具有值,但是当我尝试在FOREACH()中使用它时,我得到的是NULL值 - PHP Object has values but when I try to use it in FOREACH() I am getting NULL value 为什么当我尝试在 php 文件中使用 curl 时会被禁止 - why am i getting Forbidden when i try to use curl in php file 尝试通过脚本传递POST变量时,为什么没有收到响应? - Why am I not getting a response when I try to pass POST vars via script? 尝试打开Materialize Modal时为什么会出现此错误? - Why am I getting this error when I try to open Materialize Modal? 当我尝试在 Invoice 表上添加外键时,为什么会出现此错误? - Why am i getting this error when i try to add foreign keys on Invoice table? 为什么我在尝试运行 cron 作业时收到 mysql 扩展错误? - Why am I getting mysql extension error when I try to run a cron job? 为什么我在尝试计算 mysqli 结果时会收到警告? - Why am I getting a warning when I try to count a mysqli result? 为什么我得到空结果? - Why am I getting null results? 为什么我会得到这个? 使用未定义的常量连接 - 假定的“连接” - Why am I getting this? Use of undefined constant connection - assumed 'connection'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM