3 Replies - 649 Views - Last Post: 07 October 2012 - 11:23 AM Rate Topic: -----

#1 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Using the __autoload function

Posted 07 October 2012 - 09:43 AM

I've seen a few examples of how to use this function but most seemed to simply place the magic function at the beginning of the file that uses it.

I decided to place it in a different file, called simply autoload.php, and then I require 'autoload'.php in the file where I want to autoload the classes.

Here's the code:

autoload.php
function __autoload($classname)
{
	$filename = 'class.' . $classname . ".php"; //Note that my classes are all named class.classname.php
	require_once($filename);
}



Keyra.php (the file I want to autoload classes in)
require_once 'autoload';

//Commence to use functions from classes I haven't required at the top



I was wondering whether this was an acceptable way to go about this.

Is This A Good Question/Topic? 0
  • +

Replies To: Using the __autoload function

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: Using the __autoload function

Posted 07 October 2012 - 09:46 AM

It shouldn't make a difference. Require and include load that file right where they're called, so using them is like copy/pasting the code into the file.
Was This Post Helpful? 1
  • +
  • -

#3 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Using the __autoload function

Posted 07 October 2012 - 09:52 AM

View PostCTphpnwb, on 07 October 2012 - 09:46 AM, said:

It shouldn't make a difference. Require and include load that file right where they're called, so using them is like copy/pasting the code into the file.


Yeah, I'm just thinking whether this wouldn't lessen the lines of code needed in each file that would require these classes - but it's not a big difference in any case. I may be over complicating this by putting autoload in its own file.
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Using the __autoload function

Posted 07 October 2012 - 11:23 AM

I always used to put the autoloader into it's own file. It just seemed neater that way.

These days, however, since PHP 5.3 added namespaces, I use a different method. By organizing the classes into namespaces, and saving them in a location that matches their position in the namespace hierarchy, I can simply call spl_autoload_register() at the top of the index file (or whatever file you use as the entry point for the application), and PHP will load the files automatically when needed.

For example, if I had this code saved in "example/stuff/foo.php":
<?php
namespace example\stuff;

class Foo {
    public function bar() {
        echo "Foobar";
    }
}


Then using the spl_autoload_register() function at the top of the index file would allow the class to be call like: example\stuff\Foo() wherever needed. If you call it often, or if the path is unbearably long, you can use the use keyword at the top of scripts that use it, so you can just use the class name instead in the code.

In fact, I always do that for all classes I load from other namespaces. It just seems cleaner to have a list of imported classes at the top of the page. Kind of like it's done in other languages like Java, C# and Python. (Note that the classes aren't actually loaded when the use statement is reached, but rather when the class is used, so there is no need to be concerned that this is loading unnecessary code.)

One thing to keep in mind is that the default autoloader converts the namespace and the class name to lower case before attempting to load it, so the file path must be all lower case. - This won't affect Windows based servers, as Windows is case-insensitive by default, but on Unix servers it will cause problems.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1