Php 类扩展致命错误:找不到类“DatabaseObject”
我一直在学习LinkedIn的PHP OOP课程。当我到达继承部分时,我电脑中的代码失败了,我无法修复它 代码在第一个文件中自动加载 这是项目失败的时候。它以前从未失败过,但当我谈到继承时,它失败了 示例:B类扩展了A。这失败了 我尝试了以下方法: 将_DIR__添加到自动加载。一些警告消失,但错误仍然存在。Php 类扩展致命错误:找不到类“DatabaseObject”,php,inheritance,Php,Inheritance,我一直在学习LinkedIn的PHP OOP课程。当我到达继承部分时,我电脑中的代码失败了,我无法修复它 代码在第一个文件中自动加载 这是项目失败的时候。它以前从未失败过,但当我谈到继承时,它失败了 示例:B类扩展了A。这失败了 我尝试了以下方法: 将_DIR__添加到自动加载。一些警告消失,但错误仍然存在。 Change php tag在声明DatabaseObject类后缺少一个} 请注意,您的自动加载器没有用处,因为您只需要对/classes目录中的所有类执行一次require\u操作 它
Change php tag在声明DatabaseObject类后缺少一个}
请注意,您的自动加载器没有用处,因为您只需要对/classes目录中的所有类执行一次require\u操作 它对我有用,在glob之后添加strtoupper
// Load class definitions manually
// -> Individually
// require_once('classes/song.class.php');
// -> All classes in directory
foreach (glob (strtoupper('Controller/*.Controller.php')) as $file) {
require_once($file);
}
因此,错误消息消失了。首先,如果需要使用glob的所有类,为什么需要自动加载?只使用其中一个。其次,glob不起作用,因为glob/类位于FS的根目录中,而FS可能不在它们所在的位置。第三,您的自动加载函数是相对于其自身位置引用类dir,而该位置可能不是它们所在的位置。4您应该使用行开始^和行结束$的符号来分隔正则表达式。5 databaseobject类缺少右括号。6不鼓励使用关闭PHP标记?>。如果你提供你的目录结构,这可能会有所帮助。我不知道为什么现有的两个选项是glob和autoload。这些类都在“classes”文件夹中。我上传了一张图片。啊,那么我猜这是案件的问题。您的文件是小写的。但是类有大写字母,这是没有规则的,但实际上是的,大多数代码都用CamelCase命名类,为了一致性,这样做也很好。无论如何,我不知道LinkedIn有任何争议,但如果你的代码来自教程,我必须说教程很糟糕,如果你在网上寻找更好的教程,让LinkedIn专注于提供就业机会,你可能会做得很好……当然没问题。我知道你想在走到这一步后完成它。只需记住要有批判性,并为自己着想,因为教程可能会建议一些不是最好的方法……glob不起作用,因为给出的路径是绝对的,我怀疑他是否真的在根目录中有classes文件夹。
<?php
class DatabaseObject {
static protected $database;
static protected $table_name = "";
static protected $columns = [];
public $errors = [];
}
?>
<?php
class Bicycle extends DatabaseObject {
static protected $table_name = 'bicycles';
static protected $db_columns = ['id', 'brand', 'model', 'year', 'category', 'color', 'gender', 'price', 'weight_kg', 'condition_id', 'description'];
public $id;
public $brand;
public $model;
public $year;
public $category;
public $color;
public $description;
public $gender;
public $price;
public $weight_kg;
public $condition_id;
public const CATEGORIES = ['Road', 'Mountain', 'Hybrid', 'Cruiser', 'City', 'BMX'];
public const GENDERS = ['Mens', 'Womens', 'Unisex'];
public const CONDITION_OPTIONS = [
1 => 'Beat up',
2 => 'Decent',
3 => 'Good',
4 => 'Great',
5 => 'Like New'
];
public function __construct($args=[]) {
//$this->brand = isset($args['brand']) ? $args['brand'] : '';
$this->brand = $args['brand'] ?? '';
$this->model = $args['model'] ?? '';
$this->year = $args['year'] ?? '';
$this->category = $args['category'] ?? '';
$this->color = $args['color'] ?? '';
$this->description = $args['description'] ?? '';
$this->gender = $args['gender'] ?? '';
$this->price = $args['price'] ?? 0;
$this->weight_kg = $args['weight_kg'] ?? 0.0;
$this->condition_id = $args['condition_id'] ?? 3;
// Caution: allows private/protected properties to be set
// foreach($args as $k => $v) {
// if(property_exists($this, $k)) {
// $this->$k = $v;
// }
// }
}
?>
// Load class definitions manually
// -> Individually
// require_once('classes/song.class.php');
// -> All classes in directory
foreach (glob (strtoupper('Controller/*.Controller.php')) as $file) {
require_once($file);
}