Page 1 of 1

globals

Posted: Sun Jan 19, 2014 6:29 pm
by hallsofvallhalla
So I notice on the wiki it uses globals to define a variable in the hook file.

Code: Select all

 //Any active module that have file called myownhook.php will be ran when this code is ran.
RunHook("myownhook.php");
 
//Send a variable to the hook file, increment with one
$myVar = 100;
global $myVar 
RunHook("myownhook.php", "myVar");
echo "After hook \$myVar is $myVar";
//After hook \$myVar is 101
yet when I use the global keyword I get
Error: syntax error, unexpected '=', expecting ',' or ';'
HINT: Maybe you forget to put a ; sign at the end of a line.

Error in C:/wamp/www/ae/modules/h_mapsystem/content.php
Line 33
what am i missing. And no it is not a missing ;. I don't think I have ever used globals in PHP.

Re: globals

Posted: Sun Jan 19, 2014 6:38 pm
by KyleMassacre
I don't think you have to use globals. I have never used it and I think it was a user submitted wiki post. The only time I believe you would need to use as globals is inside a function example:

Code: Select all

RunHook("myfile.php","myvar");
function newFunction()
{
    global $myvar;
    //execution here using $myvar
}
 

Re: globals

Posted: Sun Jan 19, 2014 9:08 pm
by hallsofvallhalla
hmm so how do I pass a variable back and forth through hooks so..

content creates a empty array
i run hook and that page fills the array
then back at content i use the variable

Re: globals

Posted: Sun Jan 19, 2014 9:19 pm
by KyleMassacre
ummm lets see here.
create your file you wish to "include" and in there you would create your variables, arrays, and functions. Now what you want to do is in your content.php file do:
RunHook("yourfile.php");

That will make everything in your file readily available just like using include*. The second parameter in RunHook is used if you only want to include that particular variable.

Now if you want to pass back and forth variables to and from files you may want to look at possibly the *post_content.php files. I have never used them in that fasion so I don't know if that's something they can even do or if that's what you are even talking about haha

EDIT
Scratch the last paragraph. Maybe try something like:

Code: Select all

RunHook("myfile.php","myvar");
$emptyContentVar[$myvar["key"]];
 
Something like that?

Re: globals

Posted: Sun Jan 19, 2014 9:35 pm
by hallsofvallhalla
the runhook is not treating it as an include as I am defining the variable but I keep getting an undefined variable between content and the runhook file.

edit after your edit, let me try that :)

Re: globals

Posted: Sun Jan 19, 2014 9:38 pm
by KyleMassacre
Yeah you are getting way to deep for me my friend haha. I'm pretty basic and only know "some" of the basics here. If it doesn't work I'm sorry but that's all I got

ughh EDIT again:
Looking at your first post about the wiki and I see you said its not missing a ';' but to be sure of myself you did add one after the global variable right?

Re: globals

Posted: Sun Jan 19, 2014 9:38 pm
by hallsofvallhalla
still not working.

Re: globals

Posted: Mon Jan 20, 2014 5:05 am
by a_bertrand
Indeed you need to use global to pass variables inside through hooks. The hook files themselves will have access to the variable if you define the variables at the hook call (and no Kyle I wrote the wiki content not some user). Take the example of the inside_menu module:

I need to pass the $menuEntry variable such that the hooks will fill it. At the inside_menu level I define:

Code: Select all

global $menuEntries;
$menuEntries = array();
RunHook("menu.php", "menuEntries");
Then each modules defining the "menu.php" file, will receive the $menuEntries variable. In case it still not work hall, simply send me your files and I shall check them.

Re: globals

Posted: Mon Jan 20, 2014 1:12 pm
by KyleMassacre
maybe I need to dig a little deeper but I only remember seeing global inside a function for method

Re: globals

Posted: Mon Jan 20, 2014 3:24 pm
by hallsofvallhalla
if i use the keyword global i get the syntax error above. I can delete everything from the file and only use the one line of global and still get error.