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 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加