Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 46 revisions

[b]This is a tutorial how to integrate PEAR into Codeigniter[/b] [email=code@manjie.net]Fabian Wesner[/email]

[b]What is PEAR?[/b] [quote]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. [/quote] [url]http://pear.php.net[/url]

[b]PEAR and Codeigniter[/b] To use some of PEARs Libraries in Codeigniter you need to create a new folder. [code]system/application/pear[/code] You must copy the PEAR.php to this directory. [code]system/application/pear/PEAR.php[/code]

Then you copy some more PEAR-Directories and Classes, for example: [code]system/application/pear/HTTP/Request.php system/application/pear/Net/Socket.php system/application/pear/Net/URL.php[/code]

For the integration into Codeigniter you create a library called PearLoader. [code]system/application/libraries/PearLoader.php[/code]

PearLoader.php: [code]if (!defined('BASEPATH')) exit('No direct script access allowed');

class PearLoader{

private $pear_objects = array();

function load($package, $class){ $classname = $package."_".$class; if(array_key_exists($classname,$this->pear_objects)){ return $this->pear_objects[$classname]; } require_once("$package/$class".".php"); $this->pear_objects[$classname] = &new $classname(); return $this->pear_objects[$classname]; } }[/code]

Thats it!

[B]How to use PearLoader[/B] You can use the PearLoader in a codeigniter style. Load the Library and call $this->pearloader->load('Packagename','Classname');

The example performaes a HTTP_REQUEST to yahoo: [code]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(); } [/code]

Clone this wiki locally