Any way to avoid loading unused classes for a non-oo application?

My application uses a "central" page controller, which captures a bunch of files (I'm shy about talking to libraries), each of which contains several related classes, using require_once, before receiving a request. How in:

require_once (dir_lib . 'db.php'); require_once (dir_lib . 'uuid.php'); require_once (dir_lib . 'data.php'); require_once (dir_lib . 'token.php'); require_once (dir_lib . 'logs.php'); require_once (dir_lib . 'time.php'); 

etc...

I did not bother to check the memory usage until the recent installation of the (awesome but gigantic) HTML cleaner library and take a look at its autoloader. Apparently, the autoloader is gone, each instance of the script now weighs a colossal (sweet jesus!) 5376 kilobytes of memory. (I have no idea what the built-in autoloader is for if this is the end result, but I digress). Without an HTML cleaner, most instances still weigh more than a megabyte.

Reading about PHP autoload functions I get the impression that the autoloader is designed specifically for OOP. With the exception of the cleaner library, I use very little object-oriented code. Am I just misunderstanding this concept? Is there any other practical way to avoid blindly loading heaps of classes that might not be needed for every request? Am I just lazy trying to bring them all to the fore?

EDIT -

Repeating this comment here to clarify what I meant, non-oo, if that matters a lot:

I am mainly using classes instead of a namespace without using (hardly) the actual OOP. That is, the "DBFunctions" class may contain: For example, the "execute" and "GetRow" functions. Functions are called with a static method call, such as "DbFunctions :: Execute ($ SQL)."

+4
source share
3 answers

Is there any specific reason that these function files are not classes? The only one I can think of is compatibility with PHP4, but autoload doesn't exist at all in PHP4.

Just wrap those existing code files in class DBFunctions { ... } and prefix your function calls with DBFunctions:: to match, it should be enough for the autoloader to work (after setting the spl_autoload_register () callback), and it's worth it It is still stylistically procedural if you want to save the code this way and a worthy step towards OOPifying your entire code base if you want to go that route.

(And the HTML cleaner is an elephant, have you checked PHP inside the filter function?)

+1
source

Using the Autoload function shoudn't hit, because the class itself does not load until it is called or used. Autoload contruct basically loads the class as needed, they have not already been created.

Find the code in which the class was first created. If class files are not automatically triggered, it does not accept any memory. Perhaps not all of them were created, but they do not shut down when this is not necessary.

When you find where each of them was created, or the one you are talking about, you can use:

 //Gets the object name using the class. echo get_class($classinstance) , "\n"; 

You can handle this by placing some breakpoints using memory_get_peak_usage() .

 //Output memory usage at start of class instanciation. echo memory_get_usage() #memory_get_peak_usage() prior to 5.2.1. $classobject1 = new theclass(); //Output memory usage after class instanciation. echo memory_get_usage() 

Now try the same thing, but this time using unset () after using the class:

 //Output memory usage at start of class instanciation. echo memory_get_usage() #memory_get_peak_usage() prior to 5.2.1. $classobject1 = new theclass(); unset($classobject1); //Output memory usage after class instanciation. echo memory_get_usage() 

So, basically in theory, disable any unused instance through the application, find large objects, debug.

This is @ explanation of general comments regarding OOP:

Take this piece as a sample:

 class Tun{ private $run = 'i\'m running'; function run(){ echo $this->run; } } echo Tun::run; 

Conclusion:

 Error: Fatal error: Undefined class constant 'run' in C:\Work\pro\debug.php on line 16 

The above example, since it refers to a function that uses OOP, in this case is a private variable of the $run class. Since the class is not created (an instance of the object), it will fail. So, yes, you can use functions within a class by reference, but they basically should be a simple procedural or permalink.

I hope for this help.

+1
source

PHP5 autoload is intended only for loading classes on the fly. Only one autoload function can be used in your application (but see below). For more information, see the documents for automatic download . Basically, you can define a __autoload () function that will load any file you want (or do anything, for that matter) containing any classes you want when PHP tries to call a class that is not loaded yet.

The link you provided in your question is about autoload of the standard PHP library, which is different and more flexible. The spl_autoload_register () function allows you to register a stack of startup functions, not just one. This is most useful when you use libraries in your code that use autoload because their autoload should not depend on yours or other libraries.

If you are just starting out with OO in small projects, you probably just need __autoload (), but if you want to integrate libraries like HTMLPurifier, which uses spl_autoload, google for spl_autoload for documents, these are the first results.

+1
source

All Articles