【问题标题】:Call to undefined method in php调用 php 中未定义的方法
【发布时间】:2017-06-27 03:22:26
【问题描述】:

这是我的 config.php

  <?php

    define('DB_HOST', 'localhost');
    define('DB_NAME', 'xxxx');
    define('DB_USER', 'xxxx');
    define('DB_PASS', 'xxxx');
    ?>

它是DB.php

    <?php 
include 'config.php';


class DB {
    public static $pdo;

    public static function connection(){

        if (!isset(self::$pdo)) {

            try {

            self::$pdo = new PDO('mysql:host='.DB_HOST.'; dbname ='.DB_NAME,DB_USER, DB_PASS);
            }catch(PDOException $e){
                echo $e->getMessage();
            }

        }
        return self::$pdo;
    }


    public static function prepareOwn($sql){

        return self::connection()->prepare($sql);
    }
}



 ?>

第三个文件是Student.php

<?php 
    include 'DB.php';



    class Student {
        public $table = 'student_info';

        public function readAll(){
            $sql = "SELECT * FROM $this->table";

            $stmt = DB::prepareOwn($sql);
            $stmt->execute();
            return $stmt->fetchAll();
        }   
    }
 ?>

但是当我尝试使用 spl_autoload_register() 从 index.php 访问 readAll() 然后我可以看到 致命错误:调用未定义的方法 DB::prepareOwn() >

谁能帮我解决这个问题?

非常感谢。 萨希杜尔

【问题讨论】:

  • 你试过添加断点并单步抛出代码吗?好奇当你触发 realAll 时 DB 是什么。
  • 不,我没有。你能给我实现断点来调试问题的源代码吗??

标签: php oop pdo error-handling


【解决方案1】:

我将您的代码复制到我的代码中并看到了您的错误。但正如我猜想的那样,首先你会在 db.php 中看到这一行的错误:

return self::$pdo->prepare($sql);

致命错误:在 null 上调用成员函数 prepare()

prepare 函数从何而来? $pdo 只是这个类中的一个静态属性,它没有一个叫做 prepare 的函数!修复这条线

更新
问题是您忘记在您的 prepareOwn 中调用连接方法。所以你的新 prepareOwn 函数应该是:

public static function prepareOwn($sql) {
    self::connection();
    return self::$pdo->prepare($sql);
}

【讨论】:

  • 我在 try catch 块中实例化了 self::$pdo = new PDO。
  • 此外,如果我使用空白 prepareOwn() 方法是行不通的
  • 好的,它将是'return self::$pdo->connection($sql);'但还是同样的问题
  • 我再次检查,我注意到您的 connection() 方法从未调用过。我在 prepareOwn() 方法中添加了它,我收到了 DB Connection 的错误消息。我认为如果你这样做会很好。
  • 你能先在电脑上查看一下吗??
【解决方案2】:

我希望这段代码对你有用

class MySQLDatabase {

    // Class attributes
    private $host_name = "localhost";
    private $database_name = "XXXXXXX";
    private $database_username = "XXXXXXX";
    private $database_password = "XXXXXXX";
    private $is_connected;
    private $connection;

    private $statement ;


    // construct
    public function __construct() {

        $this->open_connection();

    }// End of construct

    // connection method
    public function open_connection() {

        try {
            $this->is_connected = TRUE ;
            // PDO Connection
            $this->connection = new PDO("mysql:host=".$this->host_name.";dbname=".$this->database_name.";charset=utf8",$this->database_username,$this->database_password);
            // Error reporting
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES,FALSE);

        } catch(PDOException $errors) {

          $this->is_connected = FALSE ;

          self::catch_errors($errors);
        }



    }// End of open connection method

    // Get connection method
    public function connection(){
        return $this->connection ;
    }

    // Close connection method
    public function close_connection() {

        $this->connection = null;

    }// End of close connection method

    private static function catch_errors($errors) {
        echo("<h4><p>" . $errors -> getMessage() . "</p></h4>");
        die();
    }


    // query method

    public function query($sql){

    return $this->statement = $this->connection->prepare($sql);

    }




}// End of database class

$database = new MySQLDatabase();





    class Student {

        protected static $table = 'My_table';

        public function readAll(){
            global $database;       
            try{

                $sql = "SELECT * FROM ". self::$table;
                $stmt = $database->query($sql);
                $stmt->execute();
                return $stmt;

            }catch(PDOException $error){

                echo("<h4><p>" . $errors -> getMessage() . "</p></h4>");
                die();

            }

        }   
    }
    $c = new Student();     
    $s = $c->readAll();
    $stmt = $s->fetchAll(PDO::FETCH_ASSOC); 

    foreach($s as $v){

        var_dump($v);

    }

【讨论】:

    猜你喜欢
    • 2012-07-27
    • 2018-04-11
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多