I’ve been following a PHP OOP course of LinkedIn. When I arrive to “Inheritance” part, the code in my PC fails and I cannot fix it.
The code has an auto load in the very first file.
This is when the project fails. It Never failed before, but when I arrive to inheritance, it fails.
Example: Class B extends A. This fails.
I’ve tried the following:
- Add
__DIR__
to the autoload. Some warnings disappear, but the error still alive. - Change php tag
<?php
to short version<?
- Change permissions to 777 (755 by default).
I expect to load the view called bicycles.php. A table with all the bicycles. This view works always, but in the moment I use inheritance, it fails.
initialize.php
<?php
foreach(glob('/classes/*.class.php') as $file) {
require_once($file);
}
// Autoload class definitions
function my_autoload($class) {
if(preg_match('/Aw+Z/', $class)) {
include('classes/' . $class . '.class.php');
}
}
spl_autoload_register('my_autoload');
$database = db_connect();
DatabaseObject::set_database($database);
?>
Parent Class: DatabaseObject
<?php
class DatabaseObject {
static protected $database;
static protected $table_name = "";
static protected $columns = [];
public $errors = [];
}
?>
Child Class: Bicycle
<?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;
// }
// }
}
?>
Errors and Warnings
Warning: include(classes/DatabaseObject.class.php): failed to open stream: No such file or directory in /home/……………./chain_gang/private/initialize.php on line 45
Warning: include(): Failed opening ‘classes/DatabaseObject.class.php’ for inclusion (include_path=’.:/usr/share/php’) in /home/…………/chain_gang/private/initialize.php on line 45
Fatal error: Uncaught Error: Class ‘DatabaseObject’ not found in /home/…………../chain_gang/private/initialize.php:51 Stack trace: #0 /home/……………./chain_gang/public/staff/bicycles/index.php(1): require_once() #1 {main} thrown in /home/……………../chain_gang/private/initialize.php on line 51
Project directory:
PS: The codes examples are not complete at all. Only the most important part I think.
2
Answers
There is a } missing after the declaration of the DatabaseObject class
Note that your autoloader is not usefull because you do a require_once of all the classes in the /classes directory 😉
It’s working for me, adding strtoupper after glob.
So the error message disappeared.