1

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?

3
  • The file is found but the namespace is wrong. Try namespace models; in MyClass.php. Commented Apr 6, 2018 at 16:04
  • 1
    You're in a namespace Geos2, plus MyClass is in MyProject namespace. What am I doing wrong? Your not using composer. Commented Apr 6, 2018 at 16:04
  • I updated the post because naming was obviously an example. Commented Apr 7, 2018 at 17:34

3 Answers 3

3

Couple of things. You have a namespace MyProject in MyClass. So you should reference the class as MyProject\MyClass.

You structure should be:

  • test.php
  • models/MyProject

    |

    • MyClass.php

test.php should be:

<?php
namespace Geos2;

set_include_path(__DIR__.'/models');

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_once($file_name);
    } else {
        echo "file not found";
    }
});

use MyProject\MyClass as MyClass;

$foo = new MyClass();
Sign up to request clarification or add additional context in comments.

Comments

0

Your namespace in the MyClass.php is wrong

You must use the correct namespace

namespace models;

Comments

0

I would suggest you to use Composer (PHP package management tool) for it instead. So after installing composer you can run composer composer initand answer couple of those project related question then run composer install . You will get a composer.json file where you can add where to find the classes . I would also suggest you to familiarize with PSR-0 and PSR-4 e.g.

{
    "autoload": {
        "psr-4": {
            "MyClass\\": "src/"
        }
    }
} 

This way if you create any class inside the source directory then it can be included(maybe not the right term) as use MyClass\Model when you have Model.php class inside the src directory . If you have a directory called Utility inside src directory and Utility has MyUtility class then you can include it as use MyClass\Utility\MyUtility and MyUtility class can have namespace MyClass\Utility.

If you run composer install then it will generate vendor directory which will contain autoload.php . This way you need not write your own autoloader every place but include it once at the entry point of the project and most php projects I have seen use this way for package management and auto loading.

These are some of my first attempts to answer questions at stackoverflow . So please correct me or suggest me if I have made mistake on my answer. I would like to improve or get feedback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.