2

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

I also tried:

require_once 'C:\wamp\www\zf_project\library\Zend\Loader.php';

and

ini_set('include_path', 
ini_get('include_path') . 
PATH_SEPARATOR . 
dirname(__FILE__). DIRECTORY_SEPARATOR. 'library');

But failed.

Then I need to load the class:

Zend_Loader::loadClass('Zend_Rest_Client');

How can I use Zend classes?

Thanks in Advance!

asdk77
  • 143
  • 1
  • 2
  • 12

2 Answers2

2

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(...);

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.

Ezequiel Muns
  • 7,492
  • 33
  • 57
  • Thank you for replying! I tried this method as: `set_include_path(implode(PATH_SEPARATOR, array('D:\wamp\www\workspace\zf_proj\library', get_include_path())); include 'Zend/Loader.php'; Zend_Loader::getInstance(); // registers autoloader Zend_Loader::loadClass('Zend_Rest_Client');//this is what I need` But error occurs: unexpected ';'(first line) Is there anything I am missing? – asdk77 Nov 27 '13 at 05:44
  • 1
    @asdk77 This answer is simply missing a closing `)` on the `set_include_path` line. I'll fix it up – Phil Nov 27 '13 at 05:58
  • Thanks! But it seems needing to use "autoloader". – asdk77 Nov 27 '13 at 16:05
  • @asdk77 I've corrected and tested :S the code now... yes I was meant to use the `Zend_Loader_Autoloader` class. – Ezequiel Muns Nov 27 '13 at 22:50
0

When you run it from web browser, the include path is set in public/index.php and then bootstrap the application. Similarly, you can copy public/index.php (e.g. as setup.php) and include it in your command line code. Also, copy the bootstrap bits that you need into that file.

Note that in ZF2 there is "console route" that let you create MVC command line script.

Here's my setup.php, notice how I load configuration with "new Zend_Config". Just 'require' this file in file you want to run from command line (console).

Edit: you have to set APPLICATION_PATH correctly in '/relative/path/to/application/'.

<?php

error_reporting(E_ALL & ~E_NOTICE | E_STRICT);
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/relative/path/to/application/'));

// Define application environment
define('APPLICATION_ENV', 'development');
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

require_once 'Zend/Application.php';

$app = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH  . '/configs/application.ini');
$res = $app->getOption('resources');
$config = new Zend_Config($res);
imel96
  • 1,385
  • 11
  • 18
  • I intend to run the script in command prompt by command lines: ../php.exe ../*.php. How can I change my script and let it to work in that way? Thanks! – asdk77 Nov 26 '13 at 16:49
  • @asdk77 added example. just require that file in your-file.php and you should be able to run it as "php your-file.php". – imel96 Nov 26 '13 at 22:17
  • Thank you for your answers! I copied the above file and saved it as 'cli.php' with the script I want to run in the same folder. Then I added "require_once 'cli.php'" at the beginning of script and run it in command prompt. Errors occur: "Fatal error: require_once(): Failed opening required 'Zend/Applic.ation.php'". Is there anything I am wrong? I am using ZF 1.12.3. – asdk77 Nov 27 '13 at 05:24
  • @asdk77 I edited the code. you need to make sure that APPLICATION_PATH is set correctly in the file. e.g. if the files are in application/scripts/ then '/relative/path/to/application/' should be '/../' – imel96 Nov 27 '13 at 06:00
  • Thanks! But it seems needing to use "autoloader" - "require_once 'Zend/Loader/Autoloader.php' "; otherwise, it still can't open the "application" stream. Thank you for your additional answers again! – asdk77 Nov 27 '13 at 16:13
  • it's hard for me to guess what's wrong. autoloader should let you load zend classes. For other classes, they should be loading too if it's configured in application.ini (autoloaderNamespaces) – imel96 Nov 27 '13 at 22:27