Pear2 require
From PEAR Wiki
Background on PEAR2 Standards require_once scheme
Contents |
[edit] Problems PEAR2 is trying to solve
- Quite a few people do not like to install a full installation program on their server just to be able to use one or two packages from PEAR
- PEAR installations are not relocatable. If you move PEAR's installation directory (containing php, data and such), chances are high that the code won't work anymore because of install-time file replacement tasks.
- PEAR currently uses require_once in nearly each file to include dependencies. This works fine, but is too slow for high-traffic pages for two reasons:
- require_once needs to traverse the whole include path until it found the correct file. This leads to at least doubled disk usage when searching for the files compared to the single access that is needed if you specify the full path.
- Checking if the file has been included only once is expensive
- A common problem when beginning with PEAR is setting up include path which is required for pear to work at all.
[edit] How PEAR2 Coding standards solve this problems
[edit] Unzip and go
To support people that just use one or two PEAR packages and do not want to install the whole installer, PEAR2 packages are able to unzip-and-go.
This means several things:
- Directories like e.h. 'data' are on predictable locations. There is no need to use PEAR_Config::get_dir() or other magic to resolve the location of data_dir.
- Pyrus (PEAR2 installer) will virtually don't do any install-time replacements to e.g. replace the package version or replace @data_dir@ variables which values are only known at install time.
- You can move your pear directory around your harddisk and still have all your packages working.
- People who don't want to install PEAR can just unzip their downloaded package file into their web app directory and start using the package. Of course, the people need to take care about dependencies themselves if they do not use Pyrus.
[edit] Getting rid of require_once
To give more control to the user in terms of speed improvements, PEAR2 standards forbid require_once and gives the user several ways to include the package files.
[edit] Include all files at once
Each package in PEAR2 will have a Package/Name/allfiles.php file that just calls 'require' on every php file the package provides. Using this method, the user will have no problems at all using the package and does not need to setup an include path or an autoloader.
Downside: Especially on driver-based packages with many files, using allfiles.php makes the application using a lot more RAM since PHP loads all the files.
[edit] Have it like it was in PEAR1
For people that want to have it all like PEAR1 and care about RAM usage, PEAR2 provides an __autoload implementation that takes care about loading the classes from the correct files. This is optional; users don't need to use it and can still use their own autoloader code.
Here we do not use require_once but have a intelligent way of just including the files that are needed.
In general, __autoload should not be slower than whole package structures using require_once.
[edit] Speed junkies
Users that need the last quantum of performance in their application will not use allfiles.php nor an autoloader. Also, such servers have opcode caches like apc, xcache or eaccelerator running.
When trying to gain performance, require_once and a large include path is not feasible. Instead, the user will have a hand-tweaked file that includes all the required PEAR package files by hand, together with a hard-coded path to not give away any cpu cycles resolving relative and include paths.
With PEAR1, this method of performance tweaking was not possible unless you altered all the installed packages by removing all require_once calls. With PEAR2, the user has the power to decide if he wants a no-brainer solution (allfiles.php), an intelligent, ram-saving class loading solution (autoloader) or a highly customized, speedy web application by doing the includes by hand.
