I know this may sound a little odd, but is it possible to create a superclass or global class which can be called from any page/class without the need to include/use?
I want to have an activity tracker and a custom error log on my site that records the activities and errors from the users but i pretty much need them on every page and every class. Rather than having to import them all the item, can you create one that is always available? Such as the core classes for PHP? (mysql_, ftp_, include_*, etc)
Thanks!
If you’re okay with including at least a single file, you could put an autoloader into an include and have that handle all of your loading for you. You can read in more detail about
spl_autoload_registerhere.From the documentation, a sample setup could be:
This code would go into your file that’s included on every page. With it, you’ll be able to do the following without ever manually including the class file:
Now, doing this assumes you have a file located at
classes/SuperClass.class.phpand it does, in fact, contain aclass SuperClass {declaration (not required, but you’ll get an error if it doesn’t).You can always change the path defined in your autoloader function, as well as the file-naming schema. Also, you can use several different locations/logic or whatever is needed to suit your setup.
For instance: