通过命令行运行php脚本时如何加载Zend类 [英] How to load Zend classes when running php script by command lines
问题描述
我有一个需要Zend类的php脚本。它可以在浏览器中运行,但是在命令提示符下通过命令行运行脚本时会发生错误。
I have a php script needing Zend classes. It can be run in a browser, but errors occur when run the script by command lines in command prompt.
require_once 'Zend/Loader.php'; // It can work in a browser but failed by command lines
我也尝试过:
require_once 'C:\wamp\www\zf_project\library\Zend\Loader.php';
和
ini_set('include_path',
ini_get('include_path') .
PATH_SEPARATOR .
dirname(__FILE__). DIRECTORY_SEPARATOR. 'library');
但是失败了。
然后我需要加载类:
Zend_Loader::loadClass('Zend_Rest_Client');
如何使用Zend类?
感谢您!
推荐答案
如果您要通过自动加载使用Zend类,而无需引导整个应用程序,需要在ZF1中进行操作(它似乎正在使用它):
If all you want is to use Zend classes via autoloading—without bootstrapping your whole application—all you need to do in ZF1 (which it what you seem to be using):
<?php
// if ZF is not in your include path to begin with
set_include_path(implode(PATH_SEPARATOR, array('/path/to/zend/library', get_include_path())));
include 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance(); // registers autoloader
// now can access Zend classes without having to include
$client = new Zend_Http_Client(...);
另外请注意,您不需要调用 Zend_Loader :: loadClass
来加载类,当您在常规代码中使用类名时,自动加载器会自动完成此操作,例如,像我上面所做的那样,调用构造函数。
Also note, you don't need to call Zend_Loader::loadClass
to load a class, this is done automatically by the autoloader when you use the class name in normal code, for example by calling the constructor as I've done above.
这篇关于通过命令行运行php脚本时如何加载Zend类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!