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

[b]This is a tutorial how to integrate PEAR into Codeigniter[/b] [email=[email protected]]Fabian Wesner[/email]

[h4]What is PEAR?[/h4] [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]

[h4]PEAR and Codeigniter[/h4] [b]Preparation[/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. Pay attention to dependencies! 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]Use PearLoader[/b] You can use the PearLoader in a codeigniter style. Load the Library and call $this->pearloader->load('[i]Packagename[/i]','[i]Classname[/i]');

The example performes 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