Ansego Posted May 31, 2014 Share Posted May 31, 2014 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... Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 31, 2014 Share Posted May 31, 2014 Registering an autoload function with spl_autoload_register() works just like registering it with __autoload(). The main difference is that spl_autoload_register() can be used to register multiple functions. Quote Link to comment Share on other sites More sharing options...
trq Posted May 31, 2014 Share Posted May 31, 2014 I wouldn't bother creating your own autoloader, composer comes with one built in, just use that. On a side note, autoloading has nothing to do with creating objects from classes. Quote Link to comment Share on other sites More sharing options...
Ansego Posted May 31, 2014 Author Share Posted May 31, 2014 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 ;-) Quote Link to comment Share on other sites More sharing options...
kicken Posted May 31, 2014 Share Posted May 31, 2014 @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. Quote Link to comment Share on other sites More sharing options...
Ansego Posted May 31, 2014 Author Share Posted May 31, 2014 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) Quote Link to comment Share on other sites More sharing options...
Solution trq Posted May 31, 2014 Solution Share Posted May 31, 2014 Given a class called Foo within the file /lib/Foo.php spl_autoload_register(function ($class) { include '/lib/' . $class . '.php'; }); $foo = new Foo; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.