2 mins read

How To create or use PHP Phar archives improve website performance

PHP Phar (PHp ARchive) is a file format used in PHP for packaging entire applications or libraries into a single archive file. Phar archives can simplify the distribution and deployment of PHP applications by bundling all necessary files into a single file, making it easier to manage and distribute applications.

Advantages of PHP Phar

Single File Distribution:

PHP Phar archives encapsulate an entire PHP application or library into a single file. This makes it easier to distribute, deploy, and manage applications.

Reduced File I/O:

Since all necessary files are bundled into a single Phar archive, there is less file I/O overhead compared to traditional file-based PHP applications. This can lead to improved performance, especially in scenarios with many file reads.

Autoloading Support:

Phar archives can include an autoloader, which simplifies the process of including classes and functions from the archive. This can enhance code organization and reduce the need for manual inclusion of files.

Compression:

Phar archives support compression, reducing the overall size of the archive and speeding up distribution. This is particularly useful when deploying applications over the network.

Disadvantages of PHP Phar:

Limited Platform Support:

PHP Phar archives may not be supported on all PHP installations. While widely supported, there might be edge cases where compatibility issues arise.

Security Concerns:

The use of PHP Phar archives introduces security considerations. If not handled carefully, Phar archives can become a potential vector for security vulnerabilities. It’s crucial to only use trusted Phar archives and validate their integrity.

Debugging Challenges:

Debugging Phar archives might be more challenging compared to debugging a traditional file-based PHP application. Extracting and examining the contents of a Phar archive during debugging can be less straightforward.

PHP Phar Example

I am showing you how to create PHP Phar Library. Here is sample code

Create file in “classes/sample1.php”

 <?  
 /**  
  * Phar demo sample Class   
  * Created By Nikunj Kansara  
  */  
 class Sample1 {  
   var $sName;  
   var $sVersion;  
   // constructor  
   function Sample1() {  
     $this->sName = 'I am Nikunj Kansara';  
     $this->sVersion = '1.0.0';  
   }  
   function getAnyContent() {  
     return '<h1>Demo Test For Phar :: getAnyContent</h1>';  
   }  
   function getContent2() {  
     return '<h2>Demo Test For Phar :: getAnyContent2</h2>';  
   }  
 }  
 ?>  

Create another file in “classes/sample2.php”

 <?  
 /**  
  * Phar demo sample Class   
  * Created By Nikunj Kansara  
  */  
 class Sample2 extends Sample1 {  
   // constructor  
   function SampleClass2() {  
     $this->sName = 'I am Sample class 2';  
     $this->sVersion = '1.0.2';  
   }  
   function getAnyContent() {  
     return '<h1>Phar Test Sample class 2</h1>';  
   }  
 }  
 ?>  

Create index file in “classes/index.php”

 <?  
 require_once('sample1.php');  
 require_once('sample2.php');  
 ?>  

Now, Create phar.php for execute

 <?  
 /**  
  * Created Demo application for test Phar features  
  * Created By Nikunj Kansara  
  */  
 //phpinfo();  
 ini_set("display_errors","1");  
 $sLibraryPath = '/var/www/demo/phar/lib/SampleLibrary.phar';  
 // we will build library in case if it not exist  
 if (! file_exists($sLibraryPath)) {  
   ini_set("phar.readonly", 0); // Could be done in php.ini  
   $oPhar = new Phar($sLibraryPath); // creating new Phar  
   $oPhar->setDefaultStub('index.php', '/classes/index.php'); // pointing main file which require all classes  
   $oPhar->buildFromDirectory('/var/www/demo/phar/classes/'); // creating our library using whole directory  
   $oPhar->compress(Phar::GZ); // plus - compressing it into gzip  
 }  
 // when library already compiled - we will using it  
 require_once('phar://'.$sLibraryPath.'.gz');  
 // demonstration of work  
 $pharClass1 = new Sample1();  
 echo $pharClass1->getAnyContent();  
 echo '<pre>';  
 print_r($pharClass1);  
 echo '</pre>';  
 $pharClass2 = new Sample2();  
 echo $pharClass2->getAnyContent();  
 echo $pharClass2->getContent2();  
 echo '<pre>';  
 print_r($pharClass2);  
 echo '</pre>';  
 ?>  
Share your Love

Leave a Reply

Your email address will not be published. Required fields are marked *