Jump to content

call spl_autoload_register() without parameters


rick645

Recommended Posts

In particular

https://www.php.net/manual/en/function.spl-autoload-register.php
The autoload function being registered. If null, then the default implementation of spl_autoload() will be registered.

In particular

https://www.php.net/manual/en/function.spl-autoload.php
This function is intended to be used as a default implementation for __autoload(). If nothing else is specified and spl_autoload_register() is called without any parameters then this function will be used for any later call to __autoload().

So what?

Intuitively, I did some tests, but I didn't understand much

$ cat main.php
<?php
spl_autoload('App');
var_dump(LOADED);

$ cat App.php
<?php
define('LOADED', true);

$ php main.php
PHP Fatal error:  Uncaught Error: Undefined constant "LOADED" in main.php:3

...mmmhhh...
It seems that the file App.php has not been loaded.

In practice, what is the use of function spl_autoload()?
What does he do?

Edited by rick645
Link to comment
Share on other sites

PHP calls the autoload function when it encounters a class name that it does not know.  The job of the autoload function is to define that class, usually by including the file which contains it.

2 hours ago, rick645 said:

ok but what means

The autoload function being registered. If null, then the default implementation of spl_autoload() will be registered.

It means that if you call

spl_autoload_register(); //Note, no parameter is passed.

without providing a function, it's treated as if you had called

spl_autoload_register('spl_autoload');

 

The default autoload function does this in a simple way, it basically takes the class name, converts it to lower case, then checks if a file with that name + an extension exists and if so, includes it.  You can control which extensions it checks by using spl_autoload_extensions.  You control which directories it looks in using the include path.

If you need more complex logic or control for loading your class files, then you would provide your own autoload function rather than use the default one.

 

Link to comment
Share on other sites

That's not really the intended way to use this stuff.  The intention is more like this:

main.php

<?php

spl_autoload_register();

$app = new Application();
$app->run();

application.php

<?php

class Application {
    public function run(){
        echo 'Running my application!', PHP_EOL;
    }
}

Then run main.php

$ php main.php
Running my application!

Calling spl_autoload_register tells PHP to use the default autoloader (spl_autoload) when it needs to autoload a class.  When PHP tries to process the new Application code, it sees that the class Application is undefined, so it calls the registered autoload functions to try and define the class.    The standard autoloader function takes the class name, makes it lowercase, then tries to locate the file using the registered extensions in the defined include paths / current directory.

If you just have a specific file you want to include, then you don't need to deal with the autoloader, just use include or require directly.

 

Link to comment
Share on other sites

 

Quote
12 hours ago, kicken said:

That's not really the intended way to use this stuff.  The intention is more like this:

 

main.php

<?php

spl_autoload_register();

$app = new Application();
$app->run();

application.php

<?php

class Application {
    public function run(){
        echo 'Running my application!', PHP_EOL;
    }
}

Then run main.php

$ php main.php
Running my application!

Calling spl_autoload_register tells PHP to use the default autoloader (spl_autoload) when it needs to autoload a class.  When PHP tries to process the new Application code, it sees that the class Application is undefined, so it calls the registered autoload functions to try and define the class.    The standard autoloader function takes the class name, makes it lowercase, then tries to locate the file using the registered extensions in the defined include paths / current directory.

If you just have a specific file you want to include, then you don't need to deal with the autoloader, just use include or require directly.

 

 

 

Link to comment
Share on other sites

12 hours ago, kicken said:

That's not really the intended way to use this stuff.  The intention is more like this:

I only did a low level test to understand how it works
 

12 hours ago, kicken said:

main.php

<?php

spl_autoload_register();

$app = new Application();
$app->run();

application.php

<?php

class Application {
    public function run(){
        echo 'Running my application!', PHP_EOL;
    }
}

 

ok, but here we are already at a higher level

Quote

The standard autoloader function takes the class name, makes it lowercase

They could have documented this more clearly

Edited by rick645
Link to comment
Share on other sites

Consider the name of the feature.  "Auto Loading" is a long standing feature of php that "automatically loads" a class simply by using it in your code.

 

<?php

$newObj = new MyClass();

 

By default, PHP has had a feature going all the way back to early versions, that would search particular directories you specified in the php.ini configuration file, looking to find a file that contains the definition for the file MyClass.

Of course this had problems, including the fact that you could only have one "MyClass" defined.  Any sophisticated PHP project, whether that be a forum, framework, cms etc., would need to keep its classes separate and distinct, and the potential for naming conflicts was significant.  This is one of the reasons PHP added namespace support, so that a component library was free to name and structure its classes in whichever way was best for the developer, and still allow its classes to be used by other developers without conflict.

Some of the leading framework and library project developers got together and formed FIG, in order to create standards documents, which they did for autoloaders in PSR-0 and then PSR-4.  

You should read those PSR's, or at least PSR-4 which is the current standard for how an autoloader should work, and how classes should be namespaced.  

At this point, because people should be using composer to manage the libraries and dependencies of their projects, and composer will generate the autoloading code, to include.  If you follow the chain of code that is generated by composer, you'll see where it calls spl_autoload to register the custom autoloader code.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.