Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use PHP namespaces with autoload?

Tags:

php

autoload

I get this error when I try to use autoload and namespaces:

Fatal error: Class 'Class1' not found in /usr/local/www/apache22/data/public/php5.3/test.php on line 10

Can anyone tell me what I am doing wrong?

Here is my code:

Class1.php:

<?php  namespace Person\Barnes\David {     class Class1     {         public function __construct()         {             echo __CLASS__;         }     } }  ?> 

test.php:

<?php  function __autoload($class) {     require $class . '.php'; }  use Person\Barnes\David;  $class = new Class1();  ?> 
like image 267
David Barnes Avatar asked Dec 02 '09 05:12

David Barnes


People also ask

How do you autoload in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

How does autoload PHP work?

The PHP Autoloader searches recursively in defined directories for class, trait and interface definitions. Without any further configuration the directory in which the requiring file resides will be used as default class path. File names don't need to obey any convention. All files are searched for class definitions.

What is the relation of Namespacing and autoloading PHP files?

Basically it says: "inside this directory, all namespaces are represented by sub directories and classes are <ClassName>. php files." Autoloading is PHP's way to automatically find classes and their corresponding files without having to require all of them manually.

What is an autoloader in PHP?

Autoloading is the process of automatically loading PHP classes without explicitly loading them with the require() , require_once() , include() , or include_once() functions. It's necessary to name your class files exactly the same as your classes. The class Views would be placed in Views.


1 Answers

Class1 is not in the global scope.

Note that this is an old answer and things have changed since the days where you couldn't assume the support for spl_autoload_register() which was introduced in PHP 5.1 (now many years ago!).

These days, you would likely be using Composer. Under the hood, this would be something along the lines of this snippet to enable class autoloading.

spl_autoload_register(function ($class) {     // Adapt this depending on your directory structure     $parts = explode('\\', $class);     include end($parts) . '.php'; }); 

For completeness, here is the old answer:

To load a class that is not defined in the global scope, you need to use an autoloader.

<?php  // Note that `__autoload()` is removed as of PHP 8 in favour of  // `spl_autoload_register()`, see above function __autoload($class) {     // Adapt this depending on your directory structure     $parts = explode('\\', $class);     require end($parts) . '.php'; }  use Person\Barnes\David as MyPerson;  $class = new MyPerson\Class1(); 

or without aliases:

use Person\Barnes\David\Class1;  $class = new Class1(); 
like image 168
tanerkay Avatar answered Sep 30 '22 18:09

tanerkay