PHP - 包含或要求在oop的类文件中不起作用

时间:2017-06-29 10:59:51

标签: php oop error-handling

如果我一直保持 DB.php 那么一切正常。

<?php
include 'config.php';
class DB {
    private static $pdo;
    private $table = 'student_info';
    public static $name;
    public static $dep;
    public static $age;
    public static function connection(){
            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);
    }

    public function readAll(){
        $sql = "SELECT * FROM $this->table";
        $stmt = self::prepareOwn($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function setValue($name, $dep, $age){
        self::$name = $name;
        self::$dep = $dep;
        self::$age = $age;
    }

    public function insertValue(){
        $sql = "INSERT INTO $this->table (name, department, age) VALUES (:name, :department, :age)";
        $stmt = self::prepareOwn($sql);
        $stmt->bindParam(':name', self::$name);
        $stmt->bindParam(':department', self::$dep);
        $stmt->bindParam(':age', self::$age);
        return $stmt->execute();
    }
}
?>

但问题是我将常量保留在单独的文件中,例如 config.php 包含或需要DB.php中的config.php文件当我尝试在 index.php 页面上使用 spl_autoload_register()时,我得到注意:使用未定义常量

我不知道究竟是什么问题。为什么不工作?我想知道逻辑。

1 个答案:

答案 0 :(得分:0)

在使用课程之前,您似乎正在使用常量。只有在您尝试使用数据库类时才会包含tree = ET.parse(this_file) # create tree from file root = tree.getroot() # set the root for x in root.iter('AvClass'): # iterates thru ALL classes for prop in x: # get 1st level properties of class # Interplay Paths for chi1 in prop: # child of prop for chi2 in chi1: # child of child if 'ATS_MM_PROJECT_DIRECTORY_NAME' in str(chi2.text): print(chi2.text) #chi2.text = 'Projects//NEW//STRING//ETC' - 因此自动加载器将包含config.php(然后还会包含DB.php)。