gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:49:31 25 4
gpt4 key购买 nike

如果我保持 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文件时,我得到 注意:当我尝试使用时使用未定义的常量 spl_autoload_register(),位于 index.php 页面。

我不知道实际上是什么问题。为什么它不起作用?我想知道逻辑。

最佳答案

看起来您在使用类之前正在使用常量。仅当您尝试使用数据库类时,才会包括config.php-因此,自动加载器将包括DB.php(然后还将包括config.php)。

关于php - PHP-包含或要求在oop的类文件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44823297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com