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?