tags:

views:

221

answers:

6

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.

+4  A: 

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')

Josh
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.
too much php
This is going to be your best bet, there is no tool to do what you want. To bad there wasn't.
MitMaro
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)
Josh
+1  A: 

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"...

jcinacio
I have a script which does something along these lines, but it gets a little hairy with namespaces.
too much php
+1  A: 

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.

Byron Whitlock
A: 

well, assuming you can drop a single file onto a computer, then it seems like you could also unzip it...

soo.. just zip your 4 files together and distribute that file. when it gets copied to the remote system, unzip it, and your directory path will be preserved (if you even need one).

You can even make zip files self extracting binaries.

If the system doesn't have php installed, it could get more complicated, but you didn't mention that scenario in your question...

Zak
+3  A: 

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.

Travis Beale
Phar does look hopeful ...
too much php
A: 

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.

Paul Biggar