I'm currently trying to package library code, which would then be shipped out to people actually trying to use that library code. After creating the PHAR file, I'm trying to verify that it was done correctly with a simple test script. At some point in the create-PHAR -> use-PHAR process, I'm doing something wrong.
How do I correctly create, and then require a PHAR file?
To keep the making and validation of the PHAR easy, I've limited everything down to a simplified version of the problem, and still cannot proceed.
Here's my files:
~/phar-creation-and-require-test/
mylibrary.php
testoflibrary.php
make-phar.php
make-phar.sh
mylibrary.phar (after being created)
mylibrary.php
contents:
<?
class FooClass {
private $foonum;
function FooClass() {
$this->foonum = 42;
}
}
?>
make-phar.php
contents:
<?php
if ($argc < 3) {
print 'You must specify files to package!';
exit(1);
}
$output_file = $argv[1];
$project_path = './';
$input_files = array_slice($argv, 2);
$phar = new Phar($output_file);
foreach ($input_files as &$input_file) {
$phar->addFile($project_path, $input_file);
}
$phar->setDefaultStub('mylibrary.php');
Which is called by make-phar.sh
:
#!/usr/bin/env bash
rm mylibrary.phar
php --define phar.readonly=0 ./make-phar.php mylibrary.phar \
phar-index.php
I can run make-phar.sh
without any errors, and mylibrary.phar
gets created.
The test script testoflibrary.php
is thus:
<?php
require_once 'phar://' . __DIR__ . '/mylibrary.phar'; // This doesn't work.
//require_once 'mylibrary.php'; // This would work, if un-commented.
$foo = new FooClass();
print_r($foo);
When I run it with php testoflibrary.php
, I get this error:
Fatal error: Class 'FooClass' not found in /Users/myusername/phar-creation-and-require-test/testoflibrary.php on line 5
To get to this state, I've been reading the docs, this tutorial, and also this tutorial. This SO question does not seem to give the information I need, nor this question, and I can't seem to find any other relevant questions/answers here on SO.
So, the question (again) is,
How do I correctly create, and then require a PHAR file?
Simply create a folder for your project, before creating a folder within there called "app" for your application's source code. Copy all of your PHP files into the app folder and make the "entrypoint" file called main. php (or you can call it anything you like and modify the script below).
Adding files one at a time (even with a single file) will fail. Just build the PHAR file from the whole directory containing all your library's source files, all at once.
e.g. With your project structure like this:
libraryproject/
src/
subfolder1/
athing.php
anotherthing.php
athing.php
anotherthing.php
phar-index.php (main file that imports the rest of the library)
make-phar.sh
make-phar.php
mylibrary.phar (after creation)
test-the-lib-works.php
The make-phar.php
script should be like this:
<?php
$phar = new Phar('mylibrary.phar');
$phar->buildFromDirectory('src/'); // This does the thing you actually want.
$phar->setDefaultStub('phar-index.php');
Then the make-phar.sh
script is this:
#!/usr/bin/env bash
rm mylibrary.phar
php --define phar.readonly=0 ./make-phar.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With