Problem with PHP autoload [migrated]

Question

I am trying to use autoload to load a class on a file but it keeps giving me this error warning message below. What can I do differently ?

The code on the autoload file is:

<?php
function my_autoload($class) {
    if(preg_match('/Aw+Z/', $class)) {
        include 'classes/'. $class.'.class.php';
    }
}

spl_autoload_register('my_autoload');

$bike = new Bicycle;
$bike->brand = 'Trek';
echo $bike->brand;
?>

The code on the file the autoload is to access is:

class Bicycle {

  var $brand;
  var $model;
  var $year;
  var $description = 'Used bicycle';
  var $weight_kg = 0.0;

  function name() {
    return $this->brand . " " . $this->model . " (" . $this->year . ")";
  }

  function weight_lbs() {
    return floatval($this->weight_kg) * 2.2046226218;
  }

  function set_weight_lbs($value) {
    $this->weight_kg = floatval($value) / 2.2046226218;
  }

}

The error message I am getting is:

[17-Nov-2020 10:13:50 UTC] PHP Warning:  include(classes/Bicycle.class.php): failed to open stream: No such file or directory in /home/gabng/public_html/autoload.php on line 4
[17-Nov-2020 10:13:50 UTC] PHP Warning:  include(): Failed opening 'classes/Bicycle.class.php' for inclusion (include_path='.:/opt/alt/php72/usr/share/pear') in /home/gabng/public_html/autoload.php on line 4
[17-Nov-2020 10:13:50 UTC] PHP Fatal error:  Uncaught Error: Class 'Bicycle' not found in /home/gabng/public_html/autoload.php:10
Stack trace:
#0 {main}
  thrown in /home/gabng/public_html/autoload.php on line 10
0
Gab 2 months 0 Answers 9 views 0

Leave an answer