doumianfeng5065 2014-03-13 23:43 采纳率: 0%
浏览 78
已采纳

为交互式PHP提供初始输入

I would like to script some initial input that I find myself repeatedly inputting when using the PHP CLI. Is this possible? I'm on a linux machine, so some form of Bash + PHP would work for me if this isn't natively supported by PHP on its own.

E.g.

php -a 

php > include "MyClassWithReallyVerboseName.php";
php > $o = new MyClassWithReallyVerboseName("The parameters are also pretty verbose");

I would like to be able to script these first two lines of PHP, so that when I execute the interactive command line the class file has already been included and the $o object will already be initialized.

Edit: My initial search did not yield any useful results, but after I posted the question I found this useful thread in the "Related" column of my post: initialising PHP interactive

The solution appears to be to include a define flag when launching interactive mode, including the special INI parameter auto_prepend_file.

php -d auto_prepend_file="/path/to/init/file" -a 
  • 写回答

2条回答 默认 最新

  • dsxjot8620 2014-03-13 23:52
    关注

    I would probably use the auto_prepend_file feature of php. Copy you php.ini file to my-project-php.ini (for instance using cp /etc/php.ini my-project-php.ini then edit the my-project-php.ini to have

    auto_prepend_file = project-includes.php
    

    the project-includes.php would have in it

    include "MyClassWithReallyVerboseName.php";
    $o = new MyClassWithReallyVerboseName("The parameters are also pretty verbose");
    

    then i would run php -c my-project-php.ini -a

    http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含