【问题标题】:Class Extends Fatal error: Class 'DatabaseObject' not found类扩展致命错误:找不到类“DatabaseObject”
【发布时间】:2019-09-05 13:30:17
【问题描述】:

我一直在关注 LinkedIn 的 PHP OOP 课程。当我到达“继承”部分时,我的电脑中的代码失败了,我无法修复它。

代码在第一个文件中有一个自动加载

这是项目失败的时候。它以前从未失败过,但是当我到达继承时,它失败了。

示例:B 类扩展 A。失败

我尝试了以下方法:

  1. __DIR__ 添加到自动加载。一些警告消失了,但错误仍然存​​在。
  2. 将 php 标签 <?php 更改为短版本 <?
  3. 将权限更改为 777(默认为 755)。

我希望加载名为bikes.php 的视图。一张桌子,里面放着所有的自行车。此视图始终有效,但在我使用继承的那一刻,它失败了。


initialize.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);
?>

父类:DatabaseObject

<?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 堆栈跟踪:#0 /home 中找不到类“DatabaseObject” /................/chain_gang/public/staff/bicycles/index.php(1): require_once() #1 {main} 在 /home/.... ............./chain_gang/private/initialize.php 第 51 行


项目目录:

PS: 代码示例根本不完整。只有我认为最重要的部分。

【问题讨论】:

  • 首先,如果您需要使用 glob 的所有类,为什么还需要自动加载?只使用其中之一。其次,glob 不会工作,因为你 glob /classes 在你的 FS 的根目录中,这可能不在它们所在的位置。第三,您的自动加载功能正在引用相对于它自己的位置的类目录,这可能不是它们所在的位置。 4 你应该用符号分隔正则表达式,行开始^ 和行结束$。 5 databaseobject 类缺少右括号。 6 不鼓励使用关闭 PHP 标记 ?&gt;。如果你提供你的目录结构会有所帮助。
  • 这段代码来自 Linkedin PHP OOP 教程。我不知道为什么存在两个选项(glob 和 autoload)。这些类都在“类”文件夹中。我上传了一张图片。
  • 啊,那我猜它的问题。您的文件是小写的。但是类有大写字母
  • 好吧,没有规则,但实际上是的,大部分代码都在 CamelCase 中命名类,为了保持一致性,这样做也很好。无论如何,我不知道 LinkedIn 有任何教程,但如果你的代码来自教程,我必须说教程很糟糕如果你在网上寻找更好的教程,你可能会做得很好,让 LinkedIn 专注于提供工作机会。 ..
  • 当然没问题。我知道当你走到这一步时你想完成它。请记住要批判并独立思考,因为教程可能会建议一些不是最好的方法......

标签: php inheritance


【解决方案1】:

DatabaseObject 类声明后缺少一个}

请注意,您的自动加载器没有用,因为您对 /classes 目录中的所有类执行了 require_once ;)

【讨论】:

  • glob 不起作用,因为给定的路径是绝对的,我怀疑他的根目录中确实有类文件夹。
【解决方案2】:

它对我有用,在 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);
}

所以错误信息消失了。

【讨论】:

    猜你喜欢
    • 2014-10-03
    • 2019-07-05
    • 2011-09-08
    • 2011-12-17
    • 2018-12-21
    • 2012-11-21
    • 2015-03-20
    • 2015-03-28
    • 2014-03-13
    相关资源
    最近更新 更多