Php 对非对象调用成员函数

Php 对非对象调用成员函数,php,scope,variable-assignment,require-once,Php,Scope,Variable Assignment,Require Once,我已经阅读了一些已经存在的带有这个或类似名称的线程。似乎没有一个能完全解决我的情况 我从我的utils.php类中得到这个错误 Notice: Undefined variable: debug in /app/www/utils.php on line 89 Fatal error: Call to a member function debug() on a non-object in /app/www/utils.php on line 89 我这样定义变量debug: require_

我已经阅读了一些已经存在的带有这个或类似名称的线程。似乎没有一个能完全解决我的情况

我从我的
utils.php
类中得到这个错误

Notice: Undefined variable: debug in /app/www/utils.php on line 89 Fatal error: Call to a member function debug() on a non-object in /app/www/utils.php on line 89
我这样定义变量
debug

require_once('PHPDebug.php');
$debug = new PHPDebug();
$debug->debug($message);
require_once('PHPDebug.php');
$debug = new PHPDebug();

function myfunction()
{
   global $debug;

   // some code

   $debug->debug($message);
}
然后这样称呼它(在第89行):

require_once('PHPDebug.php');
$debug = new PHPDebug();
$debug->debug($message);
require_once('PHPDebug.php');
$debug = new PHPDebug();

function myfunction()
{
   global $debug;

   // some code

   $debug->debug($message);
}
我之所以如此困惑,是因为我从
index.php
复制并粘贴了这些行,而这个调用工作得很好


如果您愿意,我可以包括指向
index.php
utils.php
文件的链接,以及
PHPDebug.php

感谢您的最后一条评论,您的问题的解决方案是在函数中声明
$debug
全局
。因此,您应该有如下内容:

require_once('PHPDebug.php');
$debug = new PHPDebug();
$debug->debug($message);
require_once('PHPDebug.php');
$debug = new PHPDebug();

function myfunction()
{
   global $debug;

   // some code

   $debug->debug($message);
}

您可以在中阅读有关全局的更多信息。

感谢您的最后一条评论,您的问题的解决方案是在函数中将
$debug
声明为
global
。因此,您应该有如下内容:

require_once('PHPDebug.php');
$debug = new PHPDebug();
$debug->debug($message);
require_once('PHPDebug.php');
$debug = new PHPDebug();

function myfunction()
{
   global $debug;

   // some code

   $debug->debug($message);
}

您可以在第89行do var_dump($debug)之前的。

中阅读有关全局的更多信息;我把它放在前面一行,所以新的第89行,得到:
注意:未定义变量:第89行的/app/www/utils.php中的debug NULL
是$debug的声明,就像89一样吗?好吧,你会遇到问题。由于我们不知道的原因,因为您没有发布整个代码,$debug var不再是PHPDebug的实例,而是null。因此,您不能对空变量调用方法。这可能是因为我在文件(不是类)中的任何函数之外定义$debug,然后在函数内访问$debug(在第89行等中)?首先在第89行之前执行var_dump($debug);我把它放在前面一行,所以新的第89行,得到:
注意:未定义变量:第89行的/app/www/utils.php中的debug NULL
是$debug的声明,就像89一样吗?好吧,你会遇到问题。由于我们不知道的原因,因为您没有发布整个代码,$debug var不再是PHPDebug的实例,而是null。因此,您不能对空变量调用方法。这可能是因为我在文件(不是类)中的任何函数之外定义$debug,然后在函数内访问$debug(第89行等)?