Im coming from the oldest way to include a PHP using multiple require etc...
I've tried several autoload tutorials but I get always an error:
My directory struture is very simple:
- test.php
- models
|
- MyClass.php
MyClass is very simple:
<?php
namespace MyProject;
class MyClass
{
// code
}
And test.php is:
namespace MyProject;
set_include_path (".");
spl_autoload_register(function ($class_name) {
$file_name = get_include_path()."/".$class_name . '.php';
$file_name = str_replace("\\","/",$file_name);
if( file_exists( $file_name ) ) {
echo "Trying to include ".$file_name;
require ($file_name);
}
else {
echo "file not found";
}
});
use models\MyClass as MyClass;
$foo = new MyClass();
"file not found" is not printed so the Class file is found. I got this error:
Fatal error: Uncaught Error: Class 'models\MyClass' not found in test.php
Using the oldest method with:
require ('models/MyClass.php');
$foo = new MyClass();
it works.
I searched on internet but this is the standard ways to autoload and seems works to everyone.
What am I doing wrong?
namespace models;in MyClass.php.Geos2, plus MyClass is inMyProjectnamespace. What am I doing wrong? Your not using composer.