I want to define global array constant
code in bootstrap.php
$adv_types = array('top' => 'Верх', 'left' => 'Левое', 'right' => 'Правое', 'bottom' => 'Нижнее');
code in view file
echo $form->input('Adv.type', array('type' => 'select', 'option' => $adv_types, 'label' => 'Место рекламы'));
but cakephp gives error:
"Undefined variable: adv_types"
In C++, the most common way to define a constant array should certainly be to, erm, define a constant array: const int my_array[] = {5, 6, 7, 8};
Arrays are Not Constants It defines a constant reference to an array. Because of this, we can still change the elements of a constant array.
Yes, You can define an array as constant. From PHP 5.6 onwards, it is possible to define a constant as a scalar expression, and it is also possible to define an array constant. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.
Unfortunately, the scope for bootstrap.php
is bootstrap.php
, so the $adv_types
variable will be out-of-scope as soon as PHP completes parsing bootstrap.php
.
There are several approaches you can take, depending on your actual requirements.
Solution 1: you need this variables in many of your views
If you need the variable to be available in all views, you should define and set it in AppController::beforeRender()
.
In app/app_controller.php
:
class AppController extends Controller
{
function beforeRender()
{
parent::beforeRender();
$adv_types = array('top' => 'Верх', 'left' => 'Левое', 'right' => 'Правое', 'bottom' => 'Нижнее');
$this->set(compact('adv_types'));
}
}
This will allow any of your views to access the $adv_types
array.
Solution 2: you might need access to this variable anywhere within your CakePHP app
If you must access the $adv_types
variable elsewhere in your app, you can add it to the Configure
collection. In bootstrap.php
:
Configure::write('NameOfYourAppAsNamespace.adv_types', array('top' => 'Верх', 'left' => 'Левое', 'right' => 'Правое', 'bottom' => 'Нижнее'));
I recommend using the name of your project as a pseudo namespace; adv_types
is unlikely to conflict with other identifiers, but if you start using this approach more often, your chances of creating conflicts increases. Additionally, this allows you to group the data you're storing in the Configure
collection under one namespace, which can be handy for debugging.
Anyway, this approach will allow you to access the variable in any scope under the CakePHP umbrella, by invoking Configure::read()
. Thus:
$adv_types = Configure::read('NameOfYourAppAsNamespace.adv_types');
Solution 3: you absolutely must have this variable available as a global variable
If you absolutely must have this as a standard PHP global variable, you could do the following:
$GLOBALS['adv_types'] = array('top' => 'Верх', 'left' => 'Левое', 'right' => 'Правое', 'bottom' => 'Нижнее');
Before doing this, please consider whether this is strictly necessary. Global variables are a really messy business, and you should have a clear and present need to justify it.
Edit/Update!
Thirty seconds in Google Translate has led me to discover that your array contains translations corresponding to the keys. You might want to investigate using CakePHP's Internationalization & Localization features to abstract away the distinction between English and Russian terms for top/left/right/bottom (and everything else).
These need to set in your app_controller.php, and then passed to your views
// app_controller.php
class AppController extends Controller {
var $adv_types = array('top' => 'Верх', 'left' => 'Левое', 'right' => 'Правое', 'bottom' => 'Нижнее');
function beforeFilter() {
$this->set('adv_types', $this->adv_types);
}
}
To me, bootstrap.php is not the correct file for this constant
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With