我一直在遵循LinkedIn的 PHP OOP 课程。当我到达“继承”部分时,PC中的代码将失败,并且我无法修复它。
该代码在第一个文件中具有自动加载。
这是项目失败时的情况。它从来没有失败过,但是当我继承时,它就失败了。
示例:B类扩展了A。这失败。
我尝试了以下操作:
__DIR__
添加到自动加载。一些警告消失了,但错误仍然存在。<?php
更改为简短版本<?
我希望加载名为bicycles.php的视图。有所有自行车的桌子。该视图始终有效,但是在我使用继承的那一刻,它失败了。
<?php
foreach(glob('/classes/*.class.php') as $file) {
require_once($file);
}
// Autoload class definitions
function my_autoload($class) {
if(preg_match('/\A\w+\Z/', $class)) {
include('classes/' . $class . '.class.php');
}
}
spl_autoload_register('my_autoload');
$database = db_connect();
DatabaseObject::set_database($database);
?>
<?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;
// }
// }
}
?>
警告:include(classes / DatabaseObject.class.php):无法打开流:/home/................/chain_gang/private中没有此类文件或目录/initialize.php,第45行
警告:include():无法在/ home / ....中打开'classes / DatabaseObject.class.php'以便包含(include_path ='。:/ usr / share / php')。 ../chain_gang/private/initialize.php,第45行
致命错误:未捕获错误:在/home/............../chain_gang/private/initialize.php:51中找不到类'DatabaseObject':堆栈堆栈跟踪:#0 / home /................/chain_gang/public/staff/bicycles/index.php(1):require_once()#1 {main}放在/ home /中。 ............. /第51行的chain_gang / private / initialize.php
项目目录:
PS: 代码示例根本不完整。我认为只有最重要的部分。
答案 0 :(得分:1)
在声明DatabaseObject类之后缺少}
请注意,自动加载器没有用,因为您对/ classes目录中的所有类都执行了require_once;)
答案 1 :(得分:0)
它对我有用,在 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);
}
所以错误信息消失了。