0
votes

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:

  1. Add __DIR__ to the autoload. Some warnings disappear, but the error still alive.
  2. Change php tag <?php to short version <?
  3. 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('/\A\w+\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:

enter image description here

PS: The codes examples are not complete at all. Only the most important part I think.

2
First of all why you need autoloading if you required all classes using glob? Use only one of those. Secondly, the glob Is not gonna work since you glob /classes in the root of your FS which Is probably not where they Are. Third, your autoload function Is refering the classes dir relative to Its own location which might not be where they Are. 4 you should delimit the regex with symbol for line start ^ And line end $. 5 databaseobject class Is missing closing bracket. 6 use of closing PHP tag ?> Is discouraged. It could help if you provide your directory structure. - slepic
This code it's from a Linkedin PHP OOP tutorial. I don't know why are existing two options (glob and autoload). The classes are all in 'classes' folder. I uploaded an image. - iDon'tKnoware
Ah then i Guess Its problem of the case. Your files Are in lower case. But classes have uppercase letters - slepic
Well there Is no rule for that but in practice yes, most of the code out there names classes in CamelCase, And for the sake of consistency Its good to do it too. Anyway i had no idea LinkedIn has any turorials, but if your code comes from the tutorial i must say the tutorial Is quite crappy And you might do good if you look around the web for a better one And let LinkedIn Focus on providing job opportunities... - slepic
Sure no problem. I understand you wanna Finish it when you got this far. Just remember to be critical And think for yourself as the tutorial might suggest things that Are not the best way to go... - slepic

2 Answers

1
votes

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 ;)

0
votes

It's working for me, adding strtoupper after glob.

// 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);
}

So the error message disappeared.