PEAR integration

Allan Chappell edited this page May 23, 2013 · 48 revisions

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. http://pear.php.net

PEAR and Codeigniter

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

system/application/pear

You must copy the PEAR.php to this directory.

system/application/pear/PEAR.php

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

system/application/pear/HTTP/Request.php
system/application/pear/Net/Socket.php
system/application/pear/Net/URL.php

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:

system/application/hooks/pear_hook.php
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Pear_hook{
  function index(){
  // OS independent
  ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.BASEPATH.'application/pear/');
  // 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.

system/application/libraries/Pearloader.php

Pearloader.php:

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

class Pearloader{
  function load($package, $class, $options = null){
        require_once($package.'/'.$class.'.php');
        $classname = $package."_".$class;
        if(is_null($options)){
            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://www.yahoo.com';
 $this->load->library('pearloader');
 $http_request = $this->pearloader->load('HTTP','Request');
 $http_request->setURL($url);
 $http_request->sendRequest();
 echo $http_request->getResponseBody();
}

Thanks to 4webby.com

Category:Help::Tutorials

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.