I'm getting my head around PHP autoloader. If I have the code below:
function __autoload($class) {require_once('scripts/classes/' . $class . '.class.php');}
so long as my classes are named to fit the path above should I still need to use require_once
require_once('scripts/classes/session.class.php');
Note: I'm including the __autoloading in the header() of the site (first page that is loaded. Do I need to include it on each page that is loaded to get its functionality to work? I would assume yes but I'm unsure...
thank you
You need to define __autoload
, or call spl_autoload_register
, on every page loaded in the browser. So if all your pages use the same "header", putting it in there is enough.
Once you've done that, you never need to use include
or require
anywhere else: just mention the class Session
, and PHP will use your autoload function to find the definition if it doesn't know it yet.