skip to Main Content

This is my project path configuration

./create.php

/Install/Install.php

create.php

<?php

use InstallInstall;

echo "Starting";
$install = new Install();

This gives me the error

PHP Fatal error: Uncaught Error: Class ‘InstallInstall’ not found in /project/create.php:6

Install.php

<?php

namespace Install;

class Install
{
    //whatever
}

Can someone explain me what is happening there ?

Obviously I guess that using a require_once line with my filename would probably fix the issue…but I thought using namespace and use import could prevent me from doing that like we do in classic framework like symfony / magento ?

I’ve seen some post speaking about autoloading, but i’m a little bit lost. Haven’t been able to find a clear explanation on the other stack topic neither.

2

Answers


  1. If you want to Use class from another file, you must include or require the file.

    Use require('Install.php'); before use InstallInstall;.

    If you are planning to do a big project I would recommend to use PHP frameworks rather than coding from scratch.

    Login or Signup to reply.
  2. PHP compiles code one file at a time. It doesn’t have any native concept of a "project" or a "full program".

    There are three concepts involved here, which complement rather than replacing each other:

    • Namespaces are just a way of naming things. They allow you to have two classes called Install and still tell the difference between them. The use statement just tells the compiler (within one file) which of those classes you want when you write Install. The PHP manual has a chapter on namespaces which goes into more detail on all of this.
    • Require and include are the only mechanisms that allow code in one file to reference code in another. At some point, you need to tell the compiler to load "Install.php".
    • Autoloading is a way for PHP to ask your code which file it should load, when you mention a class it hasn’t seen the definition for yet. The first time a class name is encountered, any function registered with spl_autoload_register will be called with that class name, and then has a chance to run include/require to load the definition. There is a fairly brief overview of autoloading in the PHP manual.

    So, in your example:

    • use InstallInstall; just means "when I write Install, I really mean InstallInstall"
    • new Install() is translated by the compiler to new InstallInstall()
    • the class InstallInstall hasn’t been defined; if an autoload function has been registered, it will be called, with the string "InstallInstall" as input
    • that autoload function can then look at that class name, and run require_once __DIR__ . '/some/path/Install.php';

    You can write the autoload function yourself, or you can use an "off-the-shelf" implementation where you just have to configure the directory where your classes are, and then follow a convention for how to name them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search