Ways to Include multiple classes

Hi guys,

Say for example, you are creating a CMS, how many ways are there to include multiple classes for access by its different pages?

What are their advantages and disadvantages and would it be overkill, with regards to loading time, to load all classes at all times?

I have only done OOP in C++ and all you have to do is tell each file to include a certain class header but I have realized that a Php-based CMS can be potentially more complicated because each page would have different paths to those class files, is there a practical way to avoid this?



This should put you on the right path. :wink:

PSR-0 Final Proposal - PHP Standards Working Group | Google Groups

Either I’m not understanding what you mean, or you need an autoloader - this will load classes as and when (and only when) you need them. More info here: PHP: Autoloading Classes - Manual

Yes thankyou this is what I’m looking for :slight_smile:

To understand autoloading well - I would recommend to look at different framework’s implementations.

Another common and useful practice is to write a little script to copy your base core classes into one cache file and load it up front. Symfony2 (Symfony - Home) does this. Then of course use autoload for the rest.

Ok that sounds cool, I am not currently aware how the php cache system works. I have been doing some opencart MOD’s over the last few months and it describes the following:

Basically, all information necessary to populate the template must be stored in the controller’s $data array. The native PHP extract function pulls the information out the the $data array and populates the local symbol table with it, so it is available to the template file.

Now here is the trick. The template file echoes its output; but since PHP buffering is turned on, the echoed HTML is not output to the client browser, but rather to the PHP output buffer. The fetch method stores the PHP output buffer contents in the $content variable and then returns it to the calling function. Cool beans.

Is this [QUOTE]PHP buffering /QUOTE] the same thing as the cache or is it something different, where is the best place to learn about such technicalities of the PHP api? I really struggle sometimes using PHP.net and find that other sites explain things better.

Sorry if that’s a confusing question, I’m just trying to get a clear picture of how information can be cached or buffered to a given script in the PHP language.

In this case by cache I simply mean a single php file containing multiple classes.

Usually you code one class per file just to keep things manageable. But if you know that you will almost always need certain classes then you can write a little utility program to basically cat them all into one file. Just saves some time in loading in 10 or 15 different files.

Take a look at Symfony - Home
In their app directory they have a file called bootstrap.php.cache. Nothing fancy. Just a bunch of classes.

There are of course many other ways to cache data in php. Too many to describe. But again you could look through Symfony2 for some examples. Some systems do use output buffering to save a response to a file. Then if the exact same request is made again you can just resend the file.

Thankyou for the advice, I shall check that out :slight_smile: