external_plugin and globals

Creating and modifying plugins.
Post Reply
prim8
Regular
Posts: 20
Joined: Wed Jun 08, 2005 12:48 am
Contact:

external_plugin and globals

Post by prim8 » Thu Jul 07, 2005 9:33 pm

I'm having some strange behavior related to global variables, and I wonder if anyone can comment as to what is going on here, and what config setting causes this behavior. I define an external_plugin:

Code: Select all

case 'external_plugin':
                    include 'ext.php';
                    return true;
                    break;


Code: Select all

<?
$abc = "my test";
$GLOBALS['def'] = "strange";

function printme() {
    global $abc;
    global $def;

    echo "abc is: ".$abc."<br />";
    echo "def is: ".$def."<br />";
}

printme();
?>


When I call through /plugin/ URL I get:

Code: Select all

abc is:
def is: strange


When I load ext.php directly I get:

Code: Select all

abc is: my test
def is: strange


I thought it was related to register_globals=Off, but that does not seem to be the case.

User avatar
garvinhicking
Core Developer
Posts: 30020
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: external_plugin and globals

Post by garvinhicking » Fri Jul 08, 2005 10:27 am

Your file "ext.php" is included inside the plugin's method "event_hook". That means all variables declared in ext.php are local because of that method scope.

You will need to declare the variables as global already in the plugin's event_hook method signature. The easier way for you would be to use $GLOBALS though. Makes less headache. :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

Post Reply