在命令行下执行php程序

时间:2022-11-09 00:10:26

问题


安装phpredis扩展,在php.ini中添加extension=redis.so后,编写Redis.php文件

// Redis.php
<?php
$client = new Redis();
$client->set('key', 'value');
echo $client->get('key');
....

使用php命令行执行Redis.php,但是确遇见一下问题

$ php Redis.php
$ ...." Fatal error: Class 'Redis' not found in Redis.php"...

但是,在浏览器中可以显示echo $client->get('key');的值,这是为什么?

解决过程


网上搜索


网上查找Fatal error: Class 'Redis' not found in Redis.php关键词。

终于在*找到相似提问:phpredis errors Class Redis not found in Linux

仅有的一个回复,命令行和web server可能使用不同的php.ini文件
在命令行下执行php程序

php命令行和web Server使用相同的php.ini吗?


// 去找php的doc
$ man php

// 终于找到相关信息
...
FILES
/etc/php5/cli/php.ini
The configuration file for the CLI version of PHP
/etc/php5/cgi/php.ini.
The configuration file for the CGI version of PHP
/etc/php5/apache2/php.ini.
The configuration file for the version of PHP that apache2 uses.
...

解决方案


修改的php.ini准确的说是/etc/php5/apache2/php.ini,因此仅web server可以使用phpredis扩展。
在/etc/php5/cli/php.ini中添加extension=redis.so后,执行$ php Redis.php,测试成功。