Questions tagged [spl-autoload-register]

PHP function to register a function with the Standard PHP Library (SPL) provided `__autoload` stack. If the stack is not yet activated it will be activated.

This function is to register a function with the Standard PHP Library (SPL) provided __autoload stack. If the stack is not yet activated it will be activated.

If your code has an existing __autoload() function then this function must be explicitly registered on the __autoload stack. This is because spl_autoload_register() will effectively replace the engine cache for the __autoload() function by either spl_autoload() or spl_autoload_call().

If there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined. By contrast, __autoload() may only be defined once.

Official Documentation

207 questions
39
votes
3 answers

Efficient PHP auto-loading and naming strategies

Like most web developers these days, I'm thoroughly enjoying the benefits of solid MVC architecture for web apps and sites. When doing MVC with PHP, autoloading obviously comes in extremely handy. I've become a fan of spl_autoload_register over…
zombat
  • 87,352
  • 23
  • 152
  • 163
30
votes
6 answers

PHP spl_autoload_register

I am trying to take advantage of autoloading in PHP. I have various classes in different directories, and so I have bootstrapped the autoloading as follows: function autoload_services($class_name) { $file = 'services/' . $class_name. '.php'; …
JonoB
  • 5,271
  • 14
  • 49
  • 75
20
votes
2 answers

How to use spl_autoload_register for multiple diectories in PHP?

I'm actually trying to create a MVC framework for my own, however I'm having troubles with the Autoload. It's not a problem actually, but I'd like to ask the gurus, how are they using the spl_autoload_register function when there are different…
Radical_Activity
  • 2,230
  • 5
  • 27
  • 56
20
votes
2 answers

How to use spl_autoload_register?

class Manage { spl_autoload_register(function($class) { include $class . '.class.php'; }); } Say I have some code like the above. I chose to use the anonymous function method of loading classes, but how is this used? How exactly does it…
Steve
  • 2,702
  • 4
  • 24
  • 35
11
votes
3 answers

PHP Autoloading in Namespaces

I have had a slight problem with autoloading in my namespace. As shown on the PHP manual here: http://us.php.net/manual/en/language.namespaces.rules.php you should be able to autoload namespace functions with a full qualified name e.g.…
Sammaye
  • 40,888
  • 7
  • 90
  • 139
11
votes
3 answers

Can php spl_autoload_register & composer autoloader work together?

After a little bit of research and have been unable to locate a solution to my problem. I am utilizing an API that is namespaces that I downloaded via composer. The API has it dependences that I allow composer to manage and autoload for me. Separate…
10
votes
2 answers

PHP class not found when using namespace

I am new with this namespace thing. I have 2 classes(separate files) in my base directory, say class1.php and class2.php inside a directory src/. class1.php namespace \src\utility\Timer; class Timer{ public static function somefunction(){ …
Parthapratim Neog
  • 3,593
  • 5
  • 32
  • 67
10
votes
2 answers

PHP namespacing and spl_autoload_register

I had spl_autoload_register working fine but then I decided to add some namespacing to bring in PSR2 compliance and can't seem to get it working. Directory strcuture: -index.php -classes/ -Class1.class.php -Class2.class.php …
diplosaurus
  • 2,334
  • 4
  • 19
  • 44
8
votes
5 answers

Autoload namespaces based on directory structure

According to the top comment on the PHP page spl_autoload_register( ) : Good news for PHP 5.3 users with namespaced classes: When you create a subfolder structure matching the namespaces of the >containing classes, you will never even have to…
SAz
  • 355
  • 2
  • 14
8
votes
6 answers

multiple spl_autoload_register issue

I'm working on the development of a custom framework. And I have encountered an issue when I tried to dynamise the calling of my classes. This is a visual of my files : So I decided to create a different function for each folder (libs, controllers…
Adrien G
  • 983
  • 1
  • 13
  • 31
5
votes
3 answers

PHPUnit - test autoloader class

I need to create an autoloader to my application. I don't want to depend on a file in the filesystem, so how do I mock a new call? Or how do you test an autoloader class? Thanks.
thom
  • 81
  • 3
4
votes
2 answers

How can I check if a function already exists in the SPL stack?

I am trying to use spl_autoload_register and I want to create an if() statement that will check if the class method as already been registered. For example: if (spl_autoload_function(array($this, '_loadClass')) // Then do nothing else //…
Eli
  • 4,150
  • 5
  • 41
  • 74
4
votes
1 answer

class_exists is calling spl_autoload_register

I create a simple script for autoload classes, but when I use class_exists the spl_autoload_register is executed, example:
Guilherme Nascimento
  • 7,990
  • 6
  • 41
  • 107
4
votes
1 answer

PHP finally block aborts on autoload

I have a problem with finally blocks and autoload. I am using PHP 5.5.9. Here is a minimal example:
gexicide
  • 35,369
  • 19
  • 80
  • 136
4
votes
1 answer

File Directory and autoloading issue PHP MVC

Im building a MVC PHP framework and am having some issues with autoloading my classes, which i think might be down to my file structure. Firstly, here is my file structure: Im testing developing in an Xampp localserver environment so this is why i…
Tom Burman
  • 937
  • 3
  • 14
  • 36
1
2 3
13 14