Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging multiple PHP script into a single file

Tags:

I have a PHP script which includes one or two other libraries it depends on using the 'include' statement. To make it more easily portable, I would like to somehow 'compile' the script and the included libraries it into a single PHP script (in the same way that ack includes all its Perl dependencies in one file). Is there an easy way to do this in PHP?

Clarification: Compiling to a windows executable could be (part of) an acceptable solution, but the script still needs to run on *nix, where it is better to have PHP source code with '#!/usr/bin/env php' at the top.

I want to be able to drop a single file into the $PATH somewhere on any OS and have it work without needing extra PHP libraries to be installed as well.

like image 235
too much php Avatar asked Sep 23 '09 00:09

too much php


People also ask

Can we include php file using combine function?

Instead of writing all our code in a single file, PHP allows us to INCLUDE other files, so they function as part of the current page, while keeping them separate. This is very useful for organization and not repeating code for things such as footers and navigation. Questions?

Can I include php file from another server?

Nope, this setting is disabled/not allowed by default in most web servers (php. ini) so you can not use the include to include the files from a remote addresss for security reasons.


2 Answers

Newer versions of PHP support a concept similar to jar files in java. Take a look at phar. You could package all of your application files into a single archive and run that.

like image 92
Travis Beale Avatar answered Oct 12 '22 00:10

Travis Beale


The PHP packages ScriptJoiner or even better JuggleCode might help:

  • http://packagist.org/packages/codeless/scriptjoiner
  • http://packagist.org/packages/codeless/jugglecode

Both are built upon PHP-Parser (http://packagist.org/packages/nikic/php-parser), which makes it very easy to join scriptfiles.

like image 41
codeless Avatar answered Oct 12 '22 01:10

codeless