We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Global PHP variables in Templates

Development and Programming

Lars Kirchhoff's avatar
Lars Kirchhoff
58 posts
15 years ago
Lars Kirchhoff's avatar Lars Kirchhoff

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

       
Sue Crocker's avatar
Sue Crocker
26,054 posts
15 years ago
Sue Crocker's avatar Sue Crocker

Hi, Lars. No real idea on my part.. so deferring this to the dev team. Thanks in advance for your patience.

       
Barry Cogan's avatar
Barry Cogan
291 posts
15 years ago
Barry Cogan's avatar Barry Cogan

Hi Lars,

Could you give me a bit more detail please.

Are your trying to access a PHP global in the template or an ExpressionEngine Global in php?

Thanks, Barry

       
Lars Kirchhoff's avatar
Lars Kirchhoff
58 posts
15 years ago
Lars Kirchhoff's avatar Lars Kirchhoff

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.

       
Greg Salt's avatar
Greg Salt
3,988 posts
15 years ago
Greg Salt's avatar Greg Salt

Hi Lars,

Thanks very much. We’ll update this thread as soon as we can.

Cheers

Greg

       
Lars Kirchhoff's avatar
Lars Kirchhoff
58 posts
15 years ago
Lars Kirchhoff's avatar Lars Kirchhoff

Hi Greg,

did you have some time to look at it? Any news already?

ciao Lars

       
John Henry Donovan's avatar
John Henry Donovan
12,339 posts
15 years ago
John Henry Donovan's avatar John Henry Donovan

Lars,

Do you get an error? Do you have error reporting turned on?

Can you try

<?php

require_once( PATH . '/config.php' );

?>
       
Lars Kirchhoff's avatar
Lars Kirchhoff
58 posts
15 years ago
Lars Kirchhoff's avatar Lars Kirchhoff

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

       
Ingmar's avatar
Ingmar
29,245 posts
15 years ago
Ingmar's avatar Ingmar

So, what’s not working? Are you getting an error message? Nothing? Also, moving to “Development”.

       
Lars Kirchhoff's avatar
Lars Kirchhoff
58 posts
15 years ago
Lars Kirchhoff's avatar Lars Kirchhoff

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

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.