Skip to content
Advertisement

Why auto_prepend_file take no effect in php’s interactive mode?

Install package via composer and import it :

mkdir  myproject
cd myproject
composer require metowolf/meting
mkdir public
touch public/index.php

Load it in the index.php:

cd public
vim  index.php
<?php

require __DIR__ . '/../vendor/autoload.php';
use MetowolfMeting;
$api = new Meting('netease');

Display the project directory structure:

tree myproject
myproject
├── composer.json
├── composer.lock
├── public
│   └── index.php
└── vendor
    ├── autoload.php
    ├── composer
    │   ├── autoload_classmap.php
    │   ├── autoload_namespaces.php
    │   ├── autoload_psr4.php
    │   ├── autoload_real.php
    │   ├── autoload_static.php
    │   ├── ClassLoader.php
    │   ├── installed.json
    │   └── LICENSE
    └── metowolf
        └── meting
            ├── composer.json
            ├── LICENSE
            ├── README.md
            └── src
                └── Meting.php

Verify it in browser 127.0.0.1/myproject/public,it works fine,the package Megting was loaded.

Now ,i want to load it in interactive mode:

php  -d auto_prepend_file=/home/debian/myproject/vendor/metowolf/meting/src/Meting.php  -a
Interactive mode enabled
php > use MetowolfMeting;
php > $api = new Meting('netease');
PHP Warning:  Uncaught Error: Class 'Meting' not found in php shell code:1
Stack trace:
#0 {main}
  thrown in php shell code on line 1

Why auto_prepend_file take no effect in php’s interactive mode?

Advertisement

Answer

auto_prepend_file does work in interactive shell. The issue is that the use keyword has an effect only on the current line.

With this prepend.php file:

<?php
namespace foo;

class Bar
{
    function __construct()
    {
        echo 'Success';
    }
}
?>

This works (full class name):

php -d auto_prepend_file=prepend.php -a
Interactive shell

php > new fooBar();
Success

This also works (use and new on the same line):

php -d auto_prepend_file=prepend.php -a
Interactive shell

php > use fooBar; new Bar();
Success

This fails:

php -d auto_prepend_file=prepend.php -a
Interactive shell

php > use fooBar;
php > new Bar();
PHP Warning:  Uncaught Error: Class 'Bar' not found in php shell code:1
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement