PHP:致命错误:对非对象调用成员函数

PHP:致命错误:对非对象调用成员函数,php,fatal-error,Php,Fatal Error,这里出现了一个非常奇怪的错误,我正在编写一个flatfile数据库类,在刷新之前一切正常,现在我不断收到以下消息: 致命错误:对中的非对象调用成员函数名() /home/reithg/public_html/test/engine/class.database.php在线50 我给全班打电话如下: <?php ini_set('display_errors', '1'); require('engine/class.database.php'); $config = new Confi

这里出现了一个非常奇怪的错误,我正在编写一个flatfile数据库类,在刷新之前一切正常,现在我不断收到以下消息:

致命错误:对中的非对象调用成员函数名() /home/reithg/public_html/test/engine/class.database.php在线50

我给全班打电话如下:

<?php
ini_set('display_errors', '1');

require('engine/class.database.php');

$config = new Config("lessons", array('first', 'second', 'third', 'fourth'), "data/");
$db = new Database($config, true);

print("Querying DB for 'theta' no exclusions: <br />");
print_r($db->query('theta', NULL, NULL));

print("<p /> Querying DB for 'theta' in column 'second': <br />");
print_r($db->query('theta', 'second', NULL));

print("<p /> Querying DB for first two rows: <br />");
print_r($db->getRows(2));

print("<p /> Querying DB for last three rows: <br />");
print_r($db->getRows(3, true));

print("<p /> Cleaning data for safe DB input: <br />");
$testInput = array('escape|these||delimiters','and\these\\slashes','and\0these\0nulls',"don't, forget quotes");
print("input: ");
print_r($testInput);
echo("<br />output: ");
print($db->addRow($testInput));
?>
它返回:

可捕获的致命错误:无法转换类列的对象 输入/home/reithg/public_html/test/engine/class.database.php *在线50*

这表明我正在类列的一个成员上执行
name()
,该列有一个名为
name()

当我这样做时:

print($this->_config->columns($key, "position")->name());
它返回(在foreach循环中,每次返回一个单词)

第一第二第三第四第一第二第三第四


因此,它显然是在它前面的一行工作。

我有两件事要告诉你:

  • 当你说: “我知道有很多代码,但我不明白为什么会突然发生这种情况,在一次刷新中没有对代码进行任何更改。即使我回溯到早期版本,也会发生这种情况。”我想问一下,您是否通过浏览器在PHP代码上编程?如果是这样的话,有时缓存会让您感到混乱,我发现通过PHP命令行界面开发我的PHP模型要容易得多。 如果这不是针对你的,也许考虑在开发时完全关闭浏览器浏览器。
  • 我认为您遇到的问题来自Config类的公共函数列中的语法错误。我认为您必须仔细阅读switch case的语法,您会注意到您在case-guards后面使用了分号,在这里您应该只使用冒号。也没有必要打破;在您返回后,因为您的代码无论如何都不会到达这一点

  • 我希望这有帮助-祝你好运…

    答案是由于lessons.db文件中隐藏了字符


    显示的错误与此无关,我要感谢所有花时间付出两便士的人。

    Debug-是我们每个人每天90%的工作。你也应该。很难说,伙计。您应该注意错误行,找出
    $this->\u config->columns($key,“position”)->name()
    的哪个对象不起作用。@Márcio我注意到了这一点,但是
    $this->\u config->columns($key,“position”)
    正在返回类
    Column
    的成员,该类具有方法
    name()
    @虫族这是一个突然的错误,是由于没有改变,我显然一直在尝试调试它,但当它不起作用时…?@George这是第三方代码?@Márcio这是我自己的代码分号在切换情况下是有效的,如你链接的页面所示:啊,好的:)你每天都在学习。
    <?php
        class Config {
            private
                $_db,
                $_file,
                $_columns = array(),
                $_directory,
                $_delimiter;
    
            public function __construct($file, array $columns, $directory = NULL, $delimiter = "|")  {
                $this->_db = $directory.$file.".db";
                $this->defineColumns($columns);
                $this->_directory = $directory;
                $this->_delimiter = $delimiter;
            } 
    
            public function db() {
                return $this->_db;
            }
    
            public function delimiter() {
                return $this->_delimiter;
            }       
    
            private function defineColumns($constants) {
                for ($i=0;$i<count($constants);$i++) {
                    if(in_array($constants[$i], $this->_columns))
                        die("Column names must be unique");
                    $column = new Column($constants[$i], $i);
                    $this->_columns[$column->name()] = $column;
                }
            }
    
            public function columns($index, $search = "string") {
                switch ($search) {
                    case "string";
                        return $this->_columns[$index];
                        break;
                    case "position";
                        $keys = array_keys($this->_columns);
                        return $this->_columns[$keys[$index]];
                        break;
                    default;
                        return false;
                }   
            }
        }
    ?>
    
    <?php
        class Column { 
            const
                ALL = "0",
                STRING = "1",
                NUMBER = "2",
                INT = "3",
                AUTO_INCREMENT = "4",
                CURRENT_TIME = "5";
    
            private
                $_type = ALL,
                $_name,
                $_index,
                $_maxChars = "256";
    
            public function __construct($name, $index, $type = NULL, $maxChars = NULL)  {
                $this->_name = $name;
                $this->_index = $index;
                if(!is_null($type))
                    setDataType($type);
                if(!is_null($maxChars))
                    setMaxChars($maxChars);
                return $this;
            }
    
            public function setDataType($type) {
                switch ($type) {
                    case ALL;
                    case STRING;
                    case NUMBER;
                    case INT;
                    case AUTO_INCREMENT;
                    case CURRENT_TIME;
                        $this->_type = $type;
                        break;
                    default;
                        return false;
                }
            }
    
            public function auditData($data) {
                switch ($this->_type) {
                    case ALL;
                        $output = $data;
                        break;
                    case STRING;
                        $output = (string) $data;
                        break;
                    case NUMBER;
                        $output = (float) $data;
                        break;
                    case INT;
                        $output = (int) $data;
                        break;
                    case AUTO_INCREMENT;
                        $output = (int) $data;
                        break;
                    case CURRENT_TIME;
                        $output = time();
                        break;
                    default;
                        return false;
                }
                return $output;
            }
    
            public function setMaxChars($maxChars) {
                if(is_int($maxChars)) {
                    $this->_maxChars = $maxChars;
                }
            }
    
            public function name() {
                return $this->_name;
            }
    
            public function index() {
                return $this->_index;
            }
        }
    ?>
    
    print($this->_config->columns($key, "position"));
    
    print($this->_config->columns($key, "position")->name());