【问题标题】:PHP Fatal error: Call to undefined methodPHP致命错误:调用未定义的方法
【发布时间】:2015-05-13 19:44:42
【问题描述】:

这里我导入了包含 getAllFiles() 函数的 files.class.php 类:

include_once('lib/files.class.php');

我在这里调用函数:

<?php 
  $files = new Files($db);
  $jsonEncoding = json_encode($files->getAllFiles());
  $jsonFile = fopen("jsonEncoded.json", "w") or die ("Unable to open file!");
  fwrite($jsonFile, $jsonEncoding);
  $contentOfFile = file_get_contents('./jsonEncoded.json');
  echo $contentOfFile;
  fclose($jsonFile);
?>

这是 getAllFiles() 函数:

public function getAllFiles() {
        if (!($stmt = $this->connection->prepare("SELECT ID, Latitude, Longitude, Name, Radius FROM Files"))) {
            $this->lastError = 'Failed to prepare query: ('.$this->connection->errno.') '.$this->connection->error;
            return false;
        } else {
            // Execute the query and store the result set
            $stmt->execute();
            $stmt->store_result();

        // Bind the results to variables
        $stmt->bind_result($id, $latitude, $longitude, $name, $radius);
        $results = array();
        // Keep fetching rows
        while ($stmt->fetch()) {
          // Add to array
            $results[] = array(
                'ID' => $id,
                'Latitude' => $latitude,
                'Longitude' => $longitude,
                'Name' => $name,
                'Radius' => $radius,
            );
          }
          // Return the results array
          return $results;
        }
    }

我收到以下错误:

致命错误:在第 83 行的 /var/www/localhost/htdocs/mbax4cl3/groupproject/map.php 中调用未定义的方法 Files::getAllFiles()

谢谢!

【问题讨论】:

  • 第 83 行是什么?我赌的是一个未定义的函数。
  • 确认这个方法实际上是在一个名为 Files 的类中?
  • 该方法在一个名为 Files 的类中,我确定。
  • 这是在第 83 行:$jsonEncoding = json_encode($files->getAllFiles());
  • 当你var_dump($files); 时会发生什么?

标签: php function methods undefined call


【解决方案1】:

即使它没有任何意义,问题似乎出在这一行之后的代码上: $jsonEncoding = json_encode($files->getAllFiles()); 但是,错误发生在该行。

我将代码更改为以下代码,它工作正常:

    <?php 
  $files = new Files($db);
  $jsonEncoding = json_encode($files->getAllFiles());
  print_r($jsonEncoding);
?>

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-02
    • 2011-07-17
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2017-04-01
    • 2011-04-15
    相关资源
    最近更新 更多