Zend Framework Code Camp - PHP Quebec

excitingwonderlakeInternet και Εφαρμογές Web

13 Δεκ 2013 (πριν από 4 χρόνια και 6 μήνες)

77 εμφανίσεις

Copyright © 2006, Zend Technologies Inc.

John’s Top PECL Picks

John Coggeshall

Welcome to the session

Welcome to the session!

Who am I:

John Coggeshall

Lead, North American
Professional Services

PHP 5 Core Contributor

Author: PHP 5 Unleashed

Member of Zend’s Education
Advisory Board



What is PECL?

PECL: PHP Extension Community Library

A collection of C level PHP extensions for PHP

A spin off from PEAR

PHP Extension and Application Repository

What is PECL?

Historically, the PHP community looked to PECL
as a way to manage the release process for

Allow each extension developer to maintain their
extension outside of the core PHP

Marginally successful in that regard

Practically, PECL is a collection of PHP extensions
which do not have consensus in the core

Less oversight into code quality / completeness

More like Perl’s CPAN

Using PECL extensions

Using PECL extensions is fairly straightforward

A few options are available

Option 1: Use the pecl tool

$ pecl install fileinfo

Downloads the extension, configure and compiles it for
use on your architecture

Not always available

Option 2: Compile it yourself

A more advanced approach

Complicates acquiring / installing the extension

Should work in almost every case

Compiling PECL from source

When the PECL tool is unavailable, you can
install PECL extensions by..

Downloading the extension’s latest release

Extracting the tarball

Running phpize

Creates a configuration script just for this

Compiling the extension

Creates a shared library

Enabling using the extension php.ini directive

i.e. extension=fileinfo.so

Compiling PECL from source

$ tar

zxvf mypeclext.tgz

$ cd mypeclext/

$ phpize

$ ./configure

$ make

$ make install

Compiling example:

What about Windows?

You can use PECL in PHP installations running on
Windows as well

Actually, it’s much easier

Just download the extension from the PHP web
site (pre


Once downloaded just copy to your extensions
directory and enable from php.ini

Copyright © 2006, Zend Technologies Inc.

My Favorite PECL Extensions

About my picks

My PECL Picks are based on a number of criteria,
which may or may not agree with yours

Direct experience using them

Quality of code / Trusted Developers

Interesting Emerging Technologies

For the most part my selections have to do with
data manipulation

Everyone knows about compiler caches and
debuggers already

Fileinfo Extension

Very often when uploading files you want to
verify what is being uploaded

Verifying extension isn’t enough

Most browsers lie about mime type based on the file

Fileinfo to the rescue

Detect MIME types for files based on their content

Uses a “magic” database for determining the type

Fileinfo Extension

Using Fileinfo is straightforward:

$info = new finfo(FILEINFO_MIME);

echo “The Mime Type is: “ . $info

If your “magic” library isn’t in the standard
directory you can pass the path as the second
constructor parameter:

$info = new finfo(FILEINFO_MIME, “/path/to/file/magic”);

Phar Extension

What’s Phar?

PHP Archive Files

Similar to Java .JAR files

Effectively a virtual file system tailored to PHP

Based on the PEAR PHP implementation and

Can store any type of resource you might need
for your PHP applications

Creating Phars

Once you have the Phar extension installed, the
first order of business is creating a new Phar file

Two ways to create Phars

Using streams:

‘<?php print “Hello World!”; ?>’);

Or by using a Phar object directly:

$phar = new Phar(‘/full/path/to/application.phar’, 0,


$phar[‘file.php’] = ‘<?php print “Hello World!”; ?>’;

Creating Phars

When creating Phar files using the object
approach a number of tools are available

Compression on a per
resource basis


You can also assign meta
data to any resource in the
archive, as well as the archive itself…

>setMetaData(array(‘mime’ =>

>setMetadata(array(‘bootstrap’ => ‘index.php’));

Using Phar files

Using Phar files is identical to using normal PHP
scripts in many ways

You can simply include a .phar file:

Include_once ‘library.phar’;

You can also use stream
access to load specific
resources from the Phar:

Include_once ‘phar://library.phar/myfiles/file.php’;

Note: Phar archives cannot work against remote

Using Phar files

When a Phar file is opened, you can use the
object as an array to manipulate the archive:


$phar = new Phar(‘/path/to/application.phar’, 0, ‘myapp.phar’);

// Get a PharFileInfo instance

// uses phar://myapp.phar/file.php

$phar_info = $phar[‘file.php’];

// Create a new file (or overwrite) called file.php with the contents

$phar[‘file.php’] = $contents;

// Check to see if a file exists within a phar


// Erase a file from the phar


Using Phar files

Phars can be executed directly creating a
bootstrap file..

>setMetadata(array(‘bootstrap’ => ‘index.php’));

[/home/john]$ php application.phar


/* index.php */

$p = new Phar(__FILE__);

$m = $p

require "phar://" . __FILE__ . "/" . $m["bootstrap"];




XDiff is a very useful extension for working with
different versions of a file

Very similar to the UNIX diff command

Can be used to determine the differences between
two versions of the same file

Create Patches from one file to the next

Can be combined with the likes of fileinfo/phar to
create robust package management and upgrading

Using XDiff

Creating Diffs is easy


xdiff_file_diff(“input1.txt”, “input2.txt”, “output.txt”);

Which produces an output.txt with…

1,1 +1,1 @@


+Hello, World!

Each parameter is a stream, and
xdiff_string_diff() is also available

Patching using XDiff

Just like normal UNIX diff, the output created can
be used to apply changes to the original file

1,1 +1,1 @@


+Hello, World!


$patch = file_get_contents(‘mypatch.diff’);

$original = file_get_contents(‘myfile.txt’);

$patched = xdiff_string_patch($original, $patch);

file_put_contents(‘myfilepatched.txt’, $patched);

More XDiff Goodies

It’s worthwhile to mention you can create diffs on
binary data as easily as text



Using the in
memory diffs you can store
incremental changes on just about anything

Store database schema changes, or store diffs in the

Combine with Phar to create update / rollback

Zip extension

The PECL Zip extension is a replacement for the
zip extension previously shipped with PHP

Much improved from a functional and usability

Allows for the easy creation, and reading of Zip archive

Object oriented interface

(Very) Actively maintained

Using Zip: Creating Archives

You can use Zip to create archives easily

ZipArchive::addFile() to add files

ZipArchive::addFromString() to add from PHP variables


$zip = new ZipArchive();

>open(‘myarchive.zip’, ZIPARCHIVE::CREATE);

>addFile(‘/path/to/myfile.dat’, ‘newname.dat’);

>addFromString(‘myfile.txt’, ‘This is my file…’);

print “Total files: “ . $zip



Using Zip: Reading Archives

You can read archives using the Zip extension in
a few ways

Using Streams

Using the API

Stream example:

$uncompressed = file_get_contents(‘zip://myarchive.zip#myfile.txt’);

Using Zip: Reading Archives

You can also use the API of the ZipArchive class
to extract files as well


$zip = new ZipArchive();





$zip = new ZipArchive();


>extractTo(‘.’, array(‘file1.txt’, ‘file2.txt’));


SSH2 Extension

The SSH2 extension is a rather useful little tool that
allows you to make connections to servers using
the SSH transport

controlled SSH shells

Using SFTP/SCP to transmit files back and forth between

Much more!

Using SSH2

Using SSH2 in its basic forms is a fairly
straightforward process


$connect = ssh2_connect(‘coggeshall.org’, 22);

ssh2_auth_password($connect, ‘username’, ‘password’);

$result = ssh2_exec($connect, ‘/usr/local/bin/php


while(!feof($result)) {

print fgets($result, 4096);



Note that $result is a stream which can be read
from using any stream access in PHP

Using SSH2: Secure Copy

One of the most useful bits of the ssh2 extension
is the ability to secure copy files from PHP

Very useful when transferring sensitive files to
servers at run time


$connect = ssh2_connect(‘coggeshall.org’, 22);

ssh2_auth_password($connect, ‘username’, ‘password’);

// Sending a file

ssh2_scp_send($connect, ‘/myfile.txt’, ‘/remotefile.txt’, 0755);

// Copying a file from a remote location

ssh2_scp_recv($connect, ‘/remotefile.txt’, ‘localfile.txt’);


Some final thoughts

So, for now those are my PECL picks

I’m sure they will change over time!

The important thing isn’t which extensions from
PECL you use, but more you know to use them!

Many people never even heard of PECL before

You have to be careful though, some extensions
aren’t ready for prime time

Tips on determining readiness

When looking through PECL packages, how do you know
something is of decent quality?

Many previously core extensions have been moved to PECL

Ext/dio for example

Look for core PHP devs as maintainers

Most of these extensions will, even in beta, be quality

Look for actively maintained extensions

If it hasn’t left beta in two years, or only has one release,
be careful


Extensions that are documented are likely working as

PHP Bugs

Check to see how many outstanding bugs there are for
the extension


See if anyone else has been using it with any success

Copyright © 2006, Zend Technologies Inc.


Thank you!