views:

413

answers:

1

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();
+2  A: 

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';
}
karim79
I do like this, i didn't know I could "wrap" them into a class though without knowing all the fancy ways of calling them, so I would be able to use the functions the same way I do now by just including the class name that it is in and :: inbetween the class and function name?
jasondavis
2) Also a function wrapped in 1 class would be able to be called from any page but also inside of another function that is inside another class? Like right now many of my functions use other functions inside of them, so if a function in a DB class is being called from a user class then it would still work as well? thanks for the help
jasondavis
@jasondavis - yes, SomeClass::functionName($param) is how you would call a static function.
karim79
@jasondavis - yes it will, if I understand you correctly.
karim79
this sounds too good to be true for me lol thanks a lot
jasondavis
private __construct() {...} I am a bit confused on that part, "to prevent them from being instantiated" what does this mean ?
jasondavis
@jasondavis - it means $var = new YourClass; will not work, as that class is not *meant* to be instantiated. The 'private' access modifier prevents the class from being constructed.
karim79
Ok I see so would I need to just make the 1 private function or make every function private?
jasondavis
@jasondavis - just the constructor.
karim79