Skip to content


Subversion checkout URL

You can clone with
Download ZIP

PEAR integration

Allan Chappell edited this page · 48 revisions
Clone this wiki locally

This is a tutorial how to integrate PEAR into Codeigniter by Fabian Wesner (german specialist for codeigniter )

What is PEAR?

PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide:

  • A structured library of open-source code for PHP users

  • A system for code distribution and package maintenance

  • A standard style for code written in PHP, specified here

  • The PHP Extension Community Library (PECL), see more below

  • A web site, mailing lists and download mirrors to support the PHP/PEAR community

PEAR is a community-driven project governed by its developers. PEAR's governing bodies are subdivided into the PEAR Group, Collectives, and a President. PEAR's constitution (adopted in March 2007) defining these groups is documented here. The PEAR project was founded in 1999 by Stig S. Bakken and quite a lot of people have joined the project.

PEAR and Codeigniter

1. Preparation To use some of PEARs Libraries in Codeigniter you need to create a new folder.


You must copy the PEAR.php to this directory.


Then you copy some more PEAR-Directories and Classes. Pay attention to dependencies! Example:


2. Enable Hooks Note: the purpose of the hook is to set the include path to include the path to PEAR. As an alternative to a hook, you could put the same ini_set() function in your config.php file or the main CI index.php file.

You need to enable hooks inside the config.php:

$config['enable_hooks'] = TRUE;

Open hooks.php and add a hook:

$hook['pre_controller'][] = array(
  'class' => 'Pear_hook',
  'function' => 'index',
  'filename' => 'pear_hook.php',
  'filepath' => 'hooks'

Then create a new file called pear_hook.php:

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Pear_hook{
  function index(){
  // OS independent
  // on Apache
  // ini_set('include_path',ini_get('include_path').':'.BASEPATH.'application/pear/');
  // on Windows
  // ini_set('include_path',ini_get('include_path').';'.BASEPATH.'application/pear/');


3. Use It! create a library called PearLoader.



if (!defined('BASEPATH')) exit('No direct script access allowed');

class Pearloader{
  function load($package, $class, $options = null){
        $classname = $package."_".$class;
            return new $classname();
        elseif (is_array($options)) {
            $reflector = new ReflectionClass($classname);
            return $reflector->newInstanceArgs($options);
        else {
            return new $classname($options);


Thats it!

You can use the Pearloader in a codeigniter style. Load the Library and call $this->pearloader->load('Packagename','Classname');

The example performes a HTTP_REQUEST to yahoo:

function example(){
 $url = '';
 $http_request = $this->pearloader->load('HTTP','Request');
 echo $http_request->getResponseBody();

Thanks to


Something went wrong with that request. Please try again.