Simply put, Phar is porting the Java-bound jar concept to the PHP world.
Phar can package a set of PHP files, and can create a stub (or bootstrap loader) that is executed by default, Phar can choose whether to compress, optional gzip and bzip2 formats.
The following examples illustrate how to create and use Phar:
Let's say our project name is user and contains three files:
user/user.class.php
<?PHPclassUser {Private $name= "Anonymous"; Private $email= "[Email protected]"; Public functionSet_email ($email) { $this->email=$email; } Public functionSet_name ($name) { $this->name=$name; } Public functionintroduce () {Echo"My name is$this->name and my email address is$this->email.\n "; } }
user/user.func.php
<?PHPrequire_once"User.class.php"; functionMake_user ($name,$email) { $u=Newuser (); $u->set_name ($name); $u->set_email ($email); return $u;} functionDump_user ($u) { $u-introduce ();}
user/test.php
<? PHP require_once "user.class.php"$u=new User (); $u->set_name ("Laomeng"); $u->set_email ("[Email protected]"); $u->introduce ();
We then use the following PHP program to create the Phar file:
make_phar.php
<? PHP $phar New Phar (' User.phar ', 0, ' User.phar '); $phar->buildfromdirectory (dirname(__file__). '/user '); $phar->setstub ($phar->createdefaultstub (' test.php ', ' test.php ')); $phar->compressfiles (PHAR::GZ);
After executing PHP make_phar.php, a file called User.phar can be found in the current directory.
We can directly execute the User.phar file:
PHP User.phar, this is equivalent to executing user/test.php
We can also refer to this file:
test_phar.php
<? php require_once "User.phar" require_once "phar://user.phar/user.class.php" $u =new User (); $u ->set_name ("Mengguang" $u ->set_email ("[email protected]" $u ->introduce (); require_once "phar://user.phar/user.func.php" $u =make_user ("Xiaomeng", "[email protected]" );d Ump_user ( $u );
Resources:
https://php.net/manual/en/book.phar.php
Use Phar to package and publish PHP programs