Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I autoload function files without classes in PHP?

My site is pretty large and I do not use PHP Classes, I do not understand OO good enough yet to re-write my site to use them however I would really like to use the __autoload($class_name) feature that classes use. I rely a lot on functions, I have different function files,
forums.inc.php
blogs.inc.php
user.inc.php
photos.inc.php
general.inc.php

All these files are just functions specific to a certain part of the site, except general.inc.php would have functions that need to be on ever page.

Now is there anyway I could use autoload or make something similar that would load these function files? I once thought about doing it based on the URL, like if the URL had the word forums in it, I would load the forums function file but this would not always work as ever file related to forums does not have the word forum in there URL.

Am I pretty much out of options until I learn to code OO good enough to put all my functions into classes?

//example of the autoload 
function __autoload($class_name){
    include('classes/' . $class_name . '.class.php');
}
$time = new time();
$time->fn1();
like image 955
JasonDavis Avatar asked Sep 08 '09 01:09

JasonDavis


People also ask

How do you autoload in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

What is autoload PHP used for?

Autoloading is the process of automatically loading PHP classes without explicitly loading them with the require() , require_once() , include() , or include_once() functions. It's necessary to name your class files exactly the same as your classes. The class Views would be placed in Views.

What is the relation of Namespacing and autoloading PHP files?

Basically it says: "inside this directory, all namespaces are represented by sub directories and classes are <ClassName>. php files." Autoloading is PHP's way to automatically find classes and their corresponding files without having to require all of them manually.

What is magic functions and auto loading in PHP?

In PHP __autoload() is a magic method, means it gets called automatically when you try create an object of the class and if the PHP engine doesn't find the class in the script it'll try to call __autoload() magic method. You can implement it as given below example: function __autoload($ClassName) { include($ClassName.


1 Answers

I don't think you can, to be honest, not in a straightforward way. In any case, wouldn't it be way nicer to use utility classes? There's nothing to the OOP, look at this:

<?php
    class HTMLUtil {

        public static function filter($str) {...}
        public static function entities($str) {...}
        public static function encode($str) {...}
        ...etc...
    }
?>

Static Helper/Utility classes that group together related functionality are simple to put together, just declare your functions as static:

Declaring class members or methods as static makes them accessible without needing an instantiation of the class.

Now you can use __autoload. You don't have to instantiate those classes to use any of their static functions, and it makes your code more readable (if slightly more verbose). I always find it more satisfying to do something like:

echo HTMLUtil::filter($str);

instead of:

echo filter($str); //scary filter function, where are you from?

If needed, you can also declare a private constructor in your utility classes to prevent them from being instantiated, to emphasize that their 'just a bunch of related functions':

private __construct() {...}

To call a static function from another function within the same class you would do so using the self keyword (which references the same class, and not object or class instance):

public static function foo()
{
    echo self::bar() . '..and some foo';
}
like image 50
karim79 Avatar answered Oct 29 '22 21:10

karim79