Hello,
I’m using a EE 1.7 installation and I’m having a problem with global variables in PHP code included in a template. The templates worked fine in EE 1.6.3.
<?php
require_once( BASE . '/config.php' );
?>In the config.php an object is instantiated which tries to access a global variable. it works fine on the same server, when used in a separate php file. However it does not work within the template. I know its not good coding style, but it is necessary to include old code into an EE installation.
thanks for any ideas on this.
/lars
Hey Barry,
it’s a php global in the PHP code that is included into the template. Below is simple example for the included config.php that is been shown in my first post.
<?php
class Config
{
function singleton ()
{
$conf['field1'] = "field1";
$conf['field2'] = "field2";
return $conf;
}
}
$config = Config::singleton();
echo "outside class";
print_r($config);
class DoSomething
{
function run ()
{
global $config;
echo "inside class";
print_r($config);
}
}
$do = new DoSomething();
$do->run();
?>Hope this helps a bit…
ciao Lars
Update: I’ve updated the code to work correctly stand-alone and in a template and it shows exactly the behaviour as described.
Lars,
Do you get an error? Do you have error reporting turned on?
Can you try
<?php
require_once( PATH . '/config.php' );
?>Hi John,
thanks for the reply. I’m sorry, the code is a bit confusing. BASE is a predefined directory. Error reporting is on and the config.php is included correctly. I can get output from within the config.php. I did print_r statements in various places within the config.php. I’ve updated the pseudo code example above to be a working code, that can be tested. If the config.php is now called directly from the browser, I get both print_r outputs. When the config.php is included in a template, the second print_r is empty.
best Lars
Hi Ingmar,
I’m not getting an error message. The problem is that the second print_r is empty, when used in a EE template. But I would need the information in the run method of DoSomething.
As the code works when called directly I assume, that the PHP configuration is right and globals can be accessed this way. However, when I am including the PHP file in a EE template with input PHP parsing I cannot access the $config variable anymore in the run method (hence the print_r is empty). However, the PHP code is executed in the EE template when the template is called, because the first print_r shows the correct result.
best Lars
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.