Use Phar to package and publish PHP programs

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.