Advanced PHP

Dr. Steven Bitner PEAR

PHP Extension and Application Repository PEAR  "Good write solid code, while great programmers reuse the code of good programmers." – Beginning PHP and MySQL 5 Second Edition by Jason Gilmore (chapter 11 - PEAR) What is it?  Community maintained software package  A package of packages  http://pear.php.net/ A few  Authentication  Encryption  Database abstraction  HTML_Crypt  Used in conjunction with JavaScript to pass all HTML encrypted  Logging framework  Including DB, PHP and porting to Firebug  Mail mime  Paypal API interface  XML Parser Not the only kid on the block  PEAR was one of the first, but is not the only extension package out there  Packagist (Composer) https://packagist.org/  http://symfony.com/  Don't be thrown by the .com, it's free

They're doing something right  New PHP releases tend to subsume these functions  Job postings  "Experience with xxx is a plus" Getting PEAR packages  With su rights, type ` install packageName`  I don't have such permissions on our server, and neither do you  However, you can see which packages are installed by typing `pear list`  Not much is installed for us http://xkcd.com/149/ Caching (not installed)

Using PEAR Cache Why use caching  Public facing data  Content that doesn't often change  Lots of hits  Sidebar: hits vs. pageviews vs. visits vs. visitors  Can cache portions of a page, or database images etc.

Caching pitfalls  Can be hard to troubleshoot or modify code when caching is in place  Increased overhead if used on rarely viewed pages  Individualized content  Obscure or old posts

Including the package  First PEAR and Cache_Lite need to be installed; then add the line below prior to any calls to caching functions require('Cache/Lite.');

 Since the PEAR directory is stored in your path, this relative path will work Cache id  Each cache needs an id  Can be a search term or text such as 'index', 'best of' etc. $id = ('mainPage');

Options  You can define where to store the cached content as well as how long it should last $options = ['cacheDir' => '/tmp/', 'lifeTime' => 60];  cacheDir and lifeTime are fixed indices that are used by the cache_lite package and cannot be changed Instantiating the class $cache = new Cache_Lite($options); Using or creating the cached content if ($data = $cache->get($id)) { // great, display the // cached content } else { // $page = content created with PHP $cache->save($page); }