在同一类上下文中使用时,变量为null

时间:2021-08-13 02:41:10

Writing up a class for debugging data which debugs directly to SQL, also a class. The Debug and MySQLConnection classes have worked fine until I tried to implement SQL error reporting using the Debug class the other day.

编写一个用于调试直接调试到SQL的数据的类,也是一个类。 Debug和MySQLConnection类工作正常,直到我前几天尝试使用Debug类实现SQL错误报告。

The plan is that if an error occurs during SQL execution, the MySQL class reports back to Debug which then formats it for input into the databse using the MySQL class. The errors it would input would be where the connection succeeds.

计划是,如果在SQL执行期间发生错误,MySQL类会向Debug报告,然后Debug将其格式化为使用MySQL类输入数据库。它输入的错误将是连接成功的地方。

So what I've got is as follows:

所以我得到的如下:

<?php

class Debug
{
    private $MySQL

    function __construct()
    {
        #Pass the debug object to MySQL in case we need to report errors
        $this->MySQL = new MySQLConnection($this);
    }

    public function DebugMessage($message,$code,$typeid,$SQLObj = null)
    {
        $InsertArray = array(
            #Some data, takes into account the arguments for this function
        );

        #Connect to the database and insert the data. If the connection fails, false will be returned and handled by caller. If true, everything went as planned.
        return $this->MySQL->insert('debugproc',$InsertArray);

    }
}

class MySQLConnection extends MySQLConfiguration
{
    #MySQLConfiguration is just a class containing the connection information in a protected format

    private $Debugger;
    private $SQLConn;

    function __construct($Debug)
    {
        $this->Debugger = $Debug;
    }

    public function connect()
    {
        try
        {
#This is where I get the details from the global config.
            $this->SQLConn = new PDO("mysql:host=" . config['db']['host'] . ";dbname=" . config['db']['dbname'], config['db']['user'], config['db']['pass']);
            return true;
        }
        catch (PDOException $pdo)
        {
            echo $pdo->getMessage();
            return false;
        }
    }

    public function insert($query,$data)
    {
        if($this->connect())
        {
            try
            {
                #Prepare our insert using the MySQLConfiguration array. This stores the SQL queries.
                $prepare = $this->SQLConn->prepare($this->MySQLConfiguration[$query]);
                $result = $prepare->execute($data);
                if(!$result)
                {
                    echo "Failed to insert";
                    return false;
                }
                #Release resources and disconnect from SQL
                unset($prepare);
                $this->disconnect();
            }
            catch(PDOException $pdo)
            {
                echo $pdo->getMessage();
            }

        }
        else
        {
            return false;
        }

    }
}

?>

So basically, what I'm seeing is when I construct Debug, a call is made to MySQL which contains $this, the Debug object. After that, it just awaits DebugMessage calls. When one arrives, a call should be made to $this->MySQL (MySQLConnection) to insert the relevant data. Since I can't var_dump on $this->MySQL through __construct, I echoed when the connections occur and this does happen and the connection is made successfully. As the connection is made, we should be seeing $this->MySQL's object in DebugMessage but this isn't the case, this is actually NULL!

基本上,我所看到的是当我构造Debug时,调用MySQL,其中包含$ this,Debug对象。之后,它只是等待DebugMessage调用。当一个人到达时,应该调用$ this-> MySQL(MySQLConnection)来插入相关数据。因为我不能通过__construct对$ this-> MySQL进行var_dump,所以当连接发生时我会回复,这确实发生了并且连接成功。在建立连接时,我们应该在DebugMessage中看到$ this-> MySQL的对象,但事实并非如此,这实际上是NULL!

I thought it was perhaps a visibility issue but on setting $MySQL in Debug context to public, that still showed the object as NULL during DebugMessage.

我认为这可能是一个可见性问题,但是在Debug上下文中将$ MySQL设置为public,在DebugMessage期间仍然将对象显示为NULL。

You got any ideas on this one?

你有什么想法吗?

1 个解决方案

#1


0  

I found the issue here to be some error handling in the MySQL object from a previous iteration. The calls went back to DebugMessage before the connection had been made and, as such, gave a null error on MySQL in the Debug context.

我发现这里的问题是MySQL对象中的一些错误处理。在建立连接之前,调用返回到DebugMessage,因此,在Debug上下文中对MySQL发出了null错误。

Commenting the calls to DebugMessage resolved the null variable issues.

注释对DebugMessage的调用解决了null变量问题。

What it all boiled down to was a connection which was diconnected. When disconnecting, the variable is set to null in MySQLConnection's context. When this was null, DebugMessage was getting fired before the connection was performed.

这一切都归结为一个连接的连接。断开连接时,在MySQLConnection的上下文中将变量设置为null。当它为null时,DebugMessage在执行连接之前被触发。

DOH!

#1


0  

I found the issue here to be some error handling in the MySQL object from a previous iteration. The calls went back to DebugMessage before the connection had been made and, as such, gave a null error on MySQL in the Debug context.

我发现这里的问题是MySQL对象中的一些错误处理。在建立连接之前,调用返回到DebugMessage,因此,在Debug上下文中对MySQL发出了null错误。

Commenting the calls to DebugMessage resolved the null variable issues.

注释对DebugMessage的调用解决了null变量问题。

What it all boiled down to was a connection which was diconnected. When disconnecting, the variable is set to null in MySQLConnection's context. When this was null, DebugMessage was getting fired before the connection was performed.

这一切都归结为一个连接的连接。断开连接时,在MySQLConnection的上下文中将变量设置为null。当它为null时,DebugMessage在执行连接之前被触发。

DOH!