globals

General Chat, Comments
Post Reply
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

globals

Post 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.
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: globals

Post 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
}
 
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: globals

Post 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
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: globals

Post 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?
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: globals

Post 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 :)
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: globals

Post 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?
Last edited by KyleMassacre on Sun Jan 19, 2014 9:40 pm, edited 1 time in total.
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: globals

Post by hallsofvallhalla »

still not working.
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

Re: globals

Post 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.
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: globals

Post by KyleMassacre »

maybe I need to dig a little deeper but I only remember seeing global inside a function for method
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: globals

Post 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.
Post Reply

Return to “General”