PHP Archive (.phar) Attaching with ZF2

PHP archive aka phar is a stream wrapper which can serve any packaged PHP library efficiently.

To create a phar document for a library (not for web output or for cli executable) is simple as pie like –

$phar = new \Phar('target-location-where-to-save.phar', 
 FilesystemIterator::CURRENT_AS_FILEINFO |
 FilesystemIterator::KEY_AS_FILENAME, 'optionalPharAliasName');
$phar->buildFromDirectory('source/lib/path');

After creating phar document, file can use to attach ZF2 standard autoloader as follows –

Zend\Loader\AutoloaderFactory::factory(array(
 'Zend\Loader\StandardAutoloader' => array(
   'autoregister_zf' => true,
   'namespaces' => array(
          'YourProjectNamespace'=>'phar:///absolute/path/of/project/phar/file.phar',
        ),
     ),
   ));

This works as simple folder of your file system. Easily distributable, packaged.

Posted in php, webdevelopmentTagged , , , , , , ,