Can I authorize function files without classes in PHP?

My site is quite large and I do not use PHP classes, I am not good at OO, but I am rewriting my site to use them, but I would really like to use __autoload ($ class_name), which uses classes. I rely on functions a lot, 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 specific part of the site, except that general.inc.php will have functions that should be on the page.

Anyway, can I use autoload or do something similar that will load these function files? I once thought about doing this based on the URL, for example, if the URL contained word forums, I would have downloaded the forum function file, but this did not always work, as always, the forum related file, does not contain a forum word there URL,

Am I pretty much out of options until I learn how to code OO correctly to put all my functions in classes?

//example of the autoload function __autoload($class_name){ include('classes/' . $class_name . '.class.php'); } $time = new time(); $time->fn1(); 
+6
php class autoload
source share
1 answer

I do not think that you can honestly not be straightforward. In any case, would it not be better to use utility classes? There is nothing for OOP, look at this:

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

The Static Helper / Utility classes that group related functionality are easy to combine, just declare your functions as static :

Declaring classes or methods as static makes them available without having to instantiate the class.

Now you can use __autoload . You do not need to instantiate these classes to use any of your static functions, and this makes your code more readable (if a little more verbose). I always find it more satisfactory to do something like:

 echo HTMLUtil::filter($str); 

instead:

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

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

 private __construct() {...} 

To call a static function from another function inside the same class, you would do this using the self keyword (which refers to the same class, not an instance of an object or class):

 public static function foo() { echo self::bar() . '..and some foo'; } 
+8
source share

All Articles