2

I am building a PHP CMS from the ground up. There is one super-core file within my system which I currently have automatically importing all other packages and classes that make up the core of the system. On a typical page, only a few of these classes and methods are used.

Considering the load require_once() puts on a server to include all of these files, and the time a user must wait for the page to load, I am wondering which path I should take:

  1. Keep the super-core as-is and automatically include all of the system core for each page that includes this core file.
  2. Use the super-core to include only essential packages, such as database management, and import additional packages/classes on an as-needed basis.

Could someone please let me know which of the two options are the best, as well as a brief overview of its pros and cons?

Thank you for your time!!!

mario
  • 144,265
  • 20
  • 237
  • 291
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
  • Why not just use Zend_Loader and Zend_Application to manage your application loading. Or definately look at how it is handled through Zend Framework. – Layke Jun 15 '11 at 14:23
  • 6
    Consider [PHP autoloading](http://php.net/manual/en/language.oop5.autoload.php) – Pekka Jun 15 '11 at 14:24
  • 1
    You will need to [profile](http://www.xdebug.org/docs/profiler) this. Usually a bytecode cache evens the differences out. Often only infrequently invoked sites can benefit from the load-all-at-once approach. – mario Jun 15 '11 at 14:26
  • Well, including each of the files from the system's core is a snap. I'm just looking for performance. – Oliver Spryn Jun 15 '11 at 14:26

3 Answers3

6

You're asking a question about which is the best load-strategy. This is often discussed related to auto-loaders.

As with any strategy, there are pros and cons. Including all files can save you the hassle to forget one. An autoloader on the other does not forget a file as well.

However you must not always use the one or other strategy but if you implement multiple you can choose as needed. For example if you develop your CMS things might change often. But if the CMS is installed on a server, that version does not change often.

So in production a strategy to combine all core libraries into one file and require them on startup can be a benefit depending on how much load a server has.

For an easy way to build own systems I can propose an autoloader. If you line-up your classes file by file they will get automatically loaded in the moment you use the class.

When you achieved a certain step in development you actually know what core files are or not. You can then load these by default so the autoloader would not be triggered any longer for them.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • excellent, Excellent, EXCELLENT!! Thank you, hakre! I'll look into these autoloaders, as they look like the best fit for my CMS architecture! – Oliver Spryn Jun 15 '11 at 14:31
  • @spryno724 - I added a link which links a fairly detailed article. – hakre Jun 15 '11 at 14:35
3

Earlier this year, I came upon this exact problem while developing a framework in PHP.

I considered the pros-cons and here's my evaluation:

Option 1 - Front Controller Pattern script include all other scripts

Advantages

  • Inclusion of packages are done within one script; you can see what files are included what are not at one glance.
  • Inclusion of a particular package is always called once, there is no overhead.

Disadvantages

  • Consider the case of such:

We have two classes Rectangle and Shape. Rectangle is a child class i.e. extension of Shape. However the core script includes the classes alphabetically. Thus when Rectangle is included, Shape is not found and PHP will throw an error.

Rectangle class:

class Rectangle extends Shape{

}

Shape class:

class Shape{

}
  • more overhead when everything that is not needed is also loaded into the memory.

Option 2 - Load main packages, then load other packages as-needed

Advantages

  • Files are only included when needed. Reduces overhead in another way
  • Solves the problem mentioned in Option 1.
  • You are able to concentrate on what each package requires from other packages and simply just load them

Disadvantages

  • Overhead as multiple requests for a particular package may occur.
  • Package inclusion is done in every single file.

Programming code is for human. Therefore to make things more logical and breaking down the problem, I chose option 2 to go for the framework.

Community
  • 1
  • 1
mauris
  • 42,982
  • 15
  • 99
  • 131
2

Don't load what you are not going to use. Implement an autoloader or deepen your require_once's.

Even if the performance is neglect-able, less files includes will increase your ability to quickly hunt down bugs and determine the flow of your application.

Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59