Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Autoloading with SplClassLoader?

I'm learning about namespaces in PHP 5.3 and I would like to use Namespaces Autoloading. I found this SplClassLoader class, but I can't figure out how it works.

Let's say I have directory structure like this:

system
  - framework
    - http
      - request.php
      - response.php
index.php
SplClassLoader.php

How do I enable class autoloading? What namespaces should request.php and response.php have?

This is the request.php:

namespace framework\http;

class Request
{
    public function __construct()
    {
        echo __CLASS__ . " constructer!";
    }
} 

And this is the response.php:

namespace framework\http;

class Request
{            
    public function __construct()
    {      
        echo __CLASS__ . " constructed!";                
    }           
}   

And in index.php I have:

require_once("SplClassLoader.php");
$loader = new SplClassLoader('framework\http', 'system/framework');
$loader->register();

$r = new Request();

I get this error message:

Fatal error: Class 'Request' not found in C:\wamp\apache\htdocs\php_autoloading\index.php on line 8

Why is this not working? How can I use SplClassLoader in my projects so it loads/requires my classes, and how should I setup and name folders and namespaces?

like image 604
Limeni Avatar asked Feb 23 '12 23:02

Limeni


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.

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.

What is autoload in laravel?

Auto-Loading allows you to load class files when they are needed without explicitly loading or including them. This gives you ease in running your application by loading those files automatically which are needed every time. Laravel is built to work with Composer.


1 Answers

Your file and directory names need to match the case of your classes and namespaces exactly, as in the following example:

system
  - framework
    - http
      - Request.php
      - Response.php
index.php
SplClassLoader.php

Additionally, you only need to declare the root namespace when registering the SplClassLoader object, as follows:

<?php

    require_once("SplClassLoader.php");
    $loader = new SplClassLoader('framework', 'system/framework');
    $loader->register();

    use framework\http\Request;

    $r = new Request();

?>

Hope this helps!

like image 89
rintaun Avatar answered Sep 22 '22 13:09

rintaun