20

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.

6 Answers 6

13

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.

Sign up to request clarification or add additional context in comments.

1 Comment

PHAR is pronounced as we say Mouse in Arabic. That's very funny :D
8

The PHP packages ScriptJoiner or even better JuggleCode might help:

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

1 Comment

JuggleCode replaced ScriptJoiner as of October 2012.
4

There's no built in way to do that. I would recommend packaging your code up into a directory and distributing it that way. I do this with PHP, I place the code into a directory "something.module", and then have iterate through a modules directory, including the main file underneath each .module directory. But for something more simple, you could just have a structure like:

my_package/
  my_package.php
  include1.php
  include2.php

my_package.php would include(realpath(dirname(__FILE__).'/inclue1.php')). All other scrits would just have to include('my_packahe/my_package.php')

3 Comments

So far there are at least four files, three of which are just libraries. Should the user drop them all in /usr/bin/, or do they need to work out their include_path setting and put the three libraries in one of those folders? I don't want users to have to make that choice. I just want a single file.
This is going to be your best bet, there is no tool to do what you want. To bad there wasn't.
You can't have a single file unless you do what jcinacio said. But there's no reason to worry about /usr/bin nor the include_path. Just put all necessary files, binaries, etc under one directory, and have everything in that directory include relative to FILE (a magic constant representing the path to that current file)
4

Manually, you just remove all references of include/require, and "concatenate" the files together. you should also strip the open and end tags ('<?php', "?>") before concatenation, and add them in the final file.

For one or two small libraries, it should - hopefully - "just work"...

2 Comments

I have a script which does something along these lines, but it gets a little hairy with namespaces.
If anyone does this, please remember to check if any of your included files will need to return - which would work if you do them in turn, but not if they are all in one big file.
2

phc allows this. Just run it with the --include flag, and it will combine all your code (well, every argument to an include, require, etc) into a single PHP file.

If you like, it can also compile it, but that's not required to combine them all into a single file.

1 Comment

As of 2015, PHC appears abandoned (last commit 2 years ago, July 2013).
1

You could write a custom script that opens all the files, removes the opening and closing tags, and concatenates them together and saves as one file. should be pretty easy to do.

Or you can use a php compiler. It will do a bit more than what you are looking for, but if you just want one file, you run your dev project through one of these.

You might also be able to use the built in php bytecode compiler to compile everything to byte-code and stick it in one file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.