Jump to content

PHP spl_autoload_register, spl_autoload and __autoload functions


Ansego
Go to solution Solved by trq,

Recommended Posts

Hi,

 

I've just found that __autoload function is no longer the thing to use when creating an object from your class. Well thanks a lot!!! Now we have this spl_autoload_register AND spl_autoload. All the manuals I've read I am still lost... just a simple example would be great!!!

 
 
THE OLD WAY
function __autoload($classname) {
    $filename = "./inc/". $classname .".inc";
    include_once($filename);
}

$Object_name = new Class_name();
$Object_name->Methods_name($Paramater) == Boolean_Return;
QUESTION: WHATS THE NEW WAY???
spl_autoload_register(function ($class) {
    include './inc/' . $class . '.inc';
});

$Object_name = spl_autoload_register(Class_name())
$Object_name->Methods_name($Paramater) == Boolean_Return;

Kind regards and thanks...

 

 

Link to comment
Share on other sites

Thanks Jacques1 && trq,

 

I am lost still, is my example above for the 'NEW WAY' correctly using SPL? 

 

@Jacquest1: You mentioned SPL can be used to register multiple functions unlike __AL, but are they both registering the class which has multiple functions/methods already?

 

@trq: "I wouldn't bother creating your own autoloader" thought we had to create a autoloader to use the functions/methods from the class?

@trq: 'composer' ?? did google and found: https://getcomposer.org/

@trq: Thought it was creating an object and from that object you use the functions/methods? are not all classes objects?

 

Thanks guys ;-)

 

Link to comment
Share on other sites

@Jacquest1: You mentioned SPL can be used to register multiple functions unlike __AL, but are they both registering the class which has multiple functions/methods already?

How many methods a class that gets autoloaded has is irrelevant. The only responsibility of the autoloader is to locate the class file and then include it. The main limitation of __autoload is that you can only have a single autoloader defined which is a problem if you start trying to use various libraries, each of which needs/wants to define it's own autoloader method.

 

spl_autoload_register solves this problem by allowing you to build a chain of autoloader functions which are called in sequence until either one successfully loads the class or there are no more left to call. With this setup each of the libraries that you use can just add their own autoloader function to the chain by calling spl_autoload_register with an appropriate callback.

 

 

@trq: "I wouldn't bother creating your own autoloader" thought we had to create a autoloader to use the functions/methods from the class?

@trq: 'composer' ?? did google and found: https://getcomposer.org/

You need an autoloader to dynamically include your classes. You don't necessarily have to create your own though. If you follow convention, then you can use the built-in one, spl_autoload, by just calling spl_autoload_register with no arguments.

 

For most projects you'll likely be using some external libraries and an easy way to manage those is via the composer utility. If you use it, then composer will register an autoloader to load those libraries, and you can also use it as your autoloader rather than creating your own implementation.

Link to comment
Share on other sites

Thanks @Kicken for your quick and detailed reply,

 

I've never used anything like 'composer' guess I should learn how to use it. (Tried to install it, but failed)

 

Somewhat a novice with Object (OOP)...

 

Example #1

Is this correct use of the code without the use of 'composer'?

spl_autoload_register(function ($class) {
    include './inc/' . $class . '.inc';
});
$Object_name = spl_autoload_register(Class_name())
$Object_name->Methods_name($Paramater) == Boolean_Return;
Example #2
OR do I just put this at top of every page:
spl_autoload_register(function ($class) {
    include './inc/' . $class . '.inc';
});

Though how do I call my functions / methods?

$class.METHOD_Name?

Does this mean all my classes from the inc folder are loaded in that page? (Example #2)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.