doumianfeng5065
2014-03-13 23:43
浏览 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 

图片转代码服务由CSDN问答提供 功能建议

我想编写一些初始输入,我发现自己在使用PHP CLI时反复输入。 这可能吗? 我在Linux机器上,所以如果PHP本身不支持某种形式的Bash + PHP会对我有用。

例如

  php -a 
 
php> 包括“MyClassWithReallyVerboseName.php”; 
php>  $ o = new MyClassWithReallyVerboseName(“参数也非常详细”); 
   
 
 

我希望能够编写这两行PHP的脚本,以便 当我执行交互式命令行时,已经包含了类文件,并且已经初始化了$ o对象。

编辑: 我的初始搜索没有产生任何有用的结果,但之后 我在帖子的“相关”栏中发布了我发现这个有用的帖子的问题:初始化PHP互动

解决方案似乎是在启动交互模式时包含一个define标志,包括特殊的INI参数 auto_prepend_file

  php -d auto_prepend_file =”/ path / 到/ 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

    打赏 评论
  • dongtu1789 2014-03-13 23:50

    You could create a script called e.g. "init.php" fill in the necessary initialization code and then just include it.

    打赏 评论

相关推荐 更多相似问题