26

I try to use the Silex framework as base for my web application. However, if I try to include the *.phar archive, PHP throws the following error:

Fatal error: Class 'Phar' not found in /var/www/framework/silex.phar on line 11

The following relevant lines are in my /etc/php/php.ini (as suggested in the docs of Silex):

extension=phar.so
phar.readonly = Off
phar.require_hash = Off
detect_unicode = Off

The PHAR library is present in /usr/lib/php/modules/phar.so which is set as the extension path for all libraries in my php.ini

Does anyone know why PHP is throwing this error?

SecStone
  • 1,733
  • 4
  • 20
  • 31

8 Answers8

22

Try specifying the path to the extension:

php -d extension=phar.so composer.phar <your_script>

Other options:

Based on the information you provided, there are a few possibilities:

  • You are using a different php.ini. Check the output of phpinfo() to confirm, and ensure that you are editing the active one.

  • /usr/lib/php/modules/phar.so is not readable. Ensure that the web server user can read this file.

  • Your web server has not been restarted since you last added the phar-related information to php.ini. Restart your webserver.

Community
  • 1
  • 1
George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • 1
    Thanks, I checked all your suggestions. Unfortunately, the error remains the same. – SecStone Jan 13 '12 at 13:31
  • I marked this as the correct answer as you provide a good list what could be the cause. In my case, the webserver was configured in a way that it silently failed when reading the php.ini. So I didn't detect an error some lines above the `extension=phar.so` line. – SecStone Jan 20 '12 at 15:28
  • I've never heard of PHP failing silently when it tried to read php.ini. Can you elaborate on the cause and solution? – George Cummins Jan 20 '12 at 16:21
  • Yeah, that was new to me, too. If I tried to launch a PHP script from the console (e.g. `php myscript.php`) an error returned that there was a another `.so` file missing. After commenting out this line, PHP could successfully run my script (in CLI and browser). – SecStone Jan 20 '12 at 18:59
19

This works for me:

php -d extension=phar.so composer.phar [... your command ...]

This includes the phar extension for the current runtime. Works for shared / VPC servers.

Indivision Dev
  • 1,097
  • 11
  • 16
1

On CentOS ...

  • phar.so is contained in the php-common package.
  • the phar executable is contained in the php-cli package.
  • php-mbstring and php-bz2 also seems to be required.

When php -m | grep phar returns nothing, one has to add these .ini files for the CLI:

sudo cp /etc/php-zts.d/phar.ini /etc/php-cli.d/phar.ini
sudo cp /etc/php-zts.d/mbstring.ini /etc/php-cli.d/mbstring.ini
sudo cp /etc/php-zts.d/bz2.ini /etc/php-cli.d/bz2.ini

Alternatively, one can add the same module .ini files as the webserver uses:

sudo cp /etc/php-zts.d/* /etc/php-cli.d/

It should look alike this:

$ php --ini

Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /etc/php-cli.d/
Additional .ini files parsed:      /etc/php-cli.d/bz2.ini,
/etc/php-cli.d/mbstring.ini,
/etc/php-cli.d/phar.ini

Then one can run it:

$ php ./composer.phar
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/

And move it below the $PATH:

sudo mv ./composer.phar /usr/local/bin/composer
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
1

I solved it by install phar:

apt-get install php-phar
Black
  • 18,150
  • 39
  • 158
  • 271
0

in my hosting environment i needed to specify the php version number. EG:

php-5.6 composer.phar

not

php composer.phar

Harry Bosh
  • 3,611
  • 2
  • 36
  • 34
0

Hope this may shed some light. I was using a shared host and had trouble getting composer to run. I was using this sites directions http://avantidevelopment.com/install-composer-on-bluehost/ to setup a common directory and install composer in it. I followed it but shoud add for my alias I added the following command to .bashrc

alias composer='/ramdisk/php/54/bin/php54-cli ~/common/composer.phar'

That did the trick for me. Hope whoever stumbles on this sets off a light bulb.

mmv_sat
  • 458
  • 8
  • 15
0

For Mageia 4, Mageia 5, Mageia 6 users

>> urpmi php-phar;echo done
To satisfy dependencies, the following packages are going to be installed:
  Package                        Version      Release       Arch
(medium "Core Release2")
  php-bz2                        5.6.30       2.mga6        x86_64
  php-phar                       5.6.30       2.mga6        x86_64
326KB of additional disk space will be used.
151KB of packages will be retrieved.
Proceed with the installation of the 2 packages? (Y/n) y


    $MIRRORLIST: media/core/release/php-bz2-5.6.30-2.mga6.x86_64.rpm
    $MIRRORLIST: media/core/release/php-phar-5.6.30-2.mga6.x86_64.rpm
installing php-phar-5.6.30-2.mga6.x86_64.rpm php-bz2-5.6.30-2.mga6.x86_64.rpm 
Preparing...                     ###############################################
      1/2: php-bz2               ###############################################
      2/2: php-phar              ###############################################
>> 
Paul afk
  • 87
  • 4
0

Hm. I think you need require_once 'phar://silex/silex.phar/autoload.php'; instead of require_once 'silex/silex.phar';. If this is not the case then we need to see the code throwing an error.

chx
  • 11,270
  • 7
  • 55
  • 129