views:

55

answers:

2

Is there a way how I could pass variables to Bootstrap.php? This is how I bootstrap in the index.php:

// let's go!
$application = new Zend_Application(APPLICATION_ENVIRONMENT,
                                    APPLICATION_PATH.'/configs/application.ini');

try {
    $application->bootstrap();
    $application->run();
} catch (Exception $exception) {
    echo $exception->getMessage();
    exit(1);
}

In one method of my Bootstrap.php class I need to have access to some variables from index.php and using $GLOBALS is just awful:

protected function _initFilePathConverter()
{
    require_once (PATH_TO_LIB_UTIL.'/FilePathConverter.php');
    $this->aFilePathConverter = new FilePathConverter();
    $this->aFilePathConverter->AddPathPairs($GLOBALS['REAL_PATHS'], $GLOBALS['HTTP_PATHS']);
}
+3  A: 

You can use Zend_Registry.

E.g. in the index.php you could say:

Zend_Registry::set('Real_Paths', array('my', 'paths'));

Make sure you do that after you've created the Zend_Application object, so the Zend autoloader has been initialized. And then in the bootstrap:

$real_paths = Zend_Registry::get('Real_Paths');
mercator
If the OP states that using $_GLOBALS is a pain then using Zend_Registry::set('index', $value); for setting and $value = Zend_Registry::get('index'); for getting would be more overkill.
ITroubs
Damn I forgot about this and I use Zend_Registry all the time. Thanks.
Richard Knop
I did not say it was overkill. I just called it dreadful, meaning I want to avoid using globals.
Richard Knop
@Richard I thought you would've used it already; I tend to forget about Zend_registry as well. Do you really need those globals inside the index.php itself, though? If not, you could create an application resource for them instead, so you can set them inside your application.ini.
mercator
@Richard ok then i just misunderstood.
ITroubs
@mercator Yes I need them. Company I work for has several web applications and only the newest one is built on Zend Framework (I suggested using it for our newest project). All applications share some common configuration attributes and some common config files. And we want to have these defined at just one place to avoid redundancy.
Richard Knop
You should consider writing your own Zend_Application_Resource plugin. It can access variables from the application config file or determine values programatically, rather than passing in data via the registry.
David Caunt
A: 

Try this:

index.php

<?

$testvar = 'testing';

?>

bootstrap.php

protected function _initFilePathConverter()
{
    global $testvar;
    echo $testvar;
}
ITroubs
Using global is a bad practice -1
Poru
i know that myself!
ITroubs