简体   繁体   中英

Do you really need to Auto-load ALL pages of a Class-based site on every pageload?

I've inherited a Class-based PHP site where every page of the site is its own Class. The index.php handles what page you view based on the p parameter, and then calls that specific Class. For example, the search page of the site is simply mysite.com/?p=search

One of the first lines in the index.php calls an Autoload.php file which does an include of every single Class-based page of the site. This means that no matter which page you are on, the script is loading every single other page/class.

My questions:

  1. There is NO connection between those pages/classes, so is it really necessary to load all of them on every pageload?
  2. Does it slow down the script having to include over 50 pages/classes on every pageload or is it negligible ?
  3. If so, then shouldn't I do an if-based check to determine which page/class I should load based on the p parameter and ONLY load that one rather than load every page on every pageload?
  4. Or is there some other potential downside of doing that that I'm not aware of?

Using conditional require or include is slow.

Just require_once all classes in the main file you mentioned without any conditions, right in the global scope. The compiled code will be cached as one and you won't suffer a penalty from loading extra code at runtime.

Edit: You will benefit from having PHP APC enabled or some other PHP accelerator.

Edit 2: PHP APC will become part of the core in PHP in future releases (this has been announced). You will benefit from PHP APC, regardless of wether you go with conditional loading, or load all at once. If you already have it, load all at once for your 50 pages might be just fine.

Edit 3 : Download PHP APC source code and find a find file called apc.php. Run apc.php from your server to see some very nicely done APC status and statistics, among which current memory usage and max memory (also see here ).

That's a confusing name for the file, as autoloading is a mechanism in PHP that allows you to load classes as and when they are needed, a good solution when you have many classes and only a few will be needed for each execution.

For example:

function autoload($class_name) {
  $file = "classes/$class_name.php";

  // You could add some checks here (e.g. whether the file exists, whether the
  // class indeed exists after the file is loaded) to facilitate better errors,
  // of course this would marginally increase the time needed to load each class.

  require $file;
}

// Register the `autoload` function with PHP's autoload mechanism
spl_autoload_register('autoload');

// This will execute `autoload('Class')` (unless `Class` is already defined)
$instance = new Class;

So, to answer your questions:

  1. It is not necessary to load them all, however classes that are used must be available, either by loading them all together (current situation), loading them conditionally ( if(p == 'whatever') require 'classes/whatever.php' ), or by using autoloading.

  2. There is some delay whenever a file is included as the file must be parsed/executed. PHP is fairly fast but still, including files you do not need is a waste. If you're using bytecode caching, the retrieved bytecode must still be executed.

  3. That is one avenue of improvement, autoloading presents a more dynamic alternative.

  4. Dependencies may be a problem if any of your page classes depend on another class, as your conditional loading could get very bloated.

Also a little additional material regarding bytecode caching, if you're using it:

The summary seems to be that, as long as you use include or require to load the file, bytecode caching will work as intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM