MyBB Community Forums

Full Version: Ordinary variables won't work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I've tried many times but I haven't been able to fix this... and it might be so obvious but I can't seem to find a solution.

My template looks something like this:

...
<td>{$myvar}</td>
...

And the function would be something like (it's hooked to postbit):

function foo(&$post)
{
	global $mybb;
	if ($mybb->settings['status'])
	{
		$myvar = "test";
	}
}

This doesn't work, $myvar isn't replaced. Instead, I had to do this:

...
<td>{$post['myvar']}</td>
...

function foo(&$post)
{
	global $mybb;
	if ($mybb->settings['status'])
	{
		$post['myvar'] = "test";
	}
}

This way it worked, but I don't want it to be like that! Because I'm having the same problem now when hooking my function to member_profile_end and it won't work unless I make it {$memprofile['myvar']}

What am I doing wrong?
There are mainly two different types of hooks: floating hooks and function hooks. The former are the simplest: they aren't placed in a function and consequently you can declare as many variables you want, they will inherit the scope and will be the same as they would have been declared in the core file.

main → hook → your function
  ↓                 ↓
var1 = array();  var1 = array();

Function hooks are a bit tricky to deal with. Hooks of this type are placed in a function and therefore any variable you set here will be treated as "local". This means that the main function will set his own local variables and will not inherit the ones you declare in yours. These are officially called hooks by reference because almost all of them pass one or more variables that can be edited, as it says, by reference (&$post, &$thread, ...) which can be edited and managed as you may like.

main → function → hook → your function
          ↓                    ↓
     var1 = null        var1 = array();

To workaround this you need to globalize the variables you want to use in the parent function, injecting your variables in the global scope which is accessible by the whole php code at any level.

global $var1, $var2, $var3, ...;
(2014-03-31, 10:42 AM)daakurai Wrote: [ -> ]This doesn't work, $myvar isn't replaced. Instead, I had to do this:

...
<td>{$post['myvar']}</td>
...

function foo(&$post)
{
	global $mybb;
	if ($mybb->settings['status'])
	{
		$post['myvar'] = "test";
	}
}

This way it worked, but I don't want it to be like that! Because I'm having the same problem now when hooking my function to member_profile_end and it won't work unless I make it {$memprofile['myvar']}

What am I doing wrong?

Actually that seems like the best approach IMO.
Try:

function foo(&$post)
{
    global $mybb, $myvar;

    if ($mybb->settings['status'])
    {
        $myvar = "test";
    }
} 
(2014-03-31, 01:22 PM)Shade Wrote: [ -> ]There are mainly two different types of hooks: floating hooks and function hooks. The former are the simplest: they aren't placed in a function and consequently you can declare as many variables you want, they will inherit the scope and will be the same as they would have been declared in the core file.

main → hook → your function
  ↓                 ↓
var1 = array();  var1 = array();

Function hooks are a bit tricky to deal with. Hooks of this type are placed in a function and therefore any variable you set here will be treated as "local". This means that the main function will set his own local variables and will not inherit the ones you declare in yours. These are officially called hooks by reference because almost all of them pass one or more variables that can be edited, as it says, by reference (&$post, &$thread, ...) which can be edited and managed as you may like.

main → function → hook → your function
          ↓                    ↓
     var1 = null        var1 = array();

To workaround this you need to globalize the variables you want to use in the parent function, injecting your variables in the global scope which is accessible by the whole php code at any level.

global $var1, $var2, $var3, ...;

I haven't really tested that but my interpretation of your posts leads me to believe that you're incorrect.

A 'hook' is something MyBB creates, it's not directly provided by PHP (would be awesome if we could change the addresses of functions so everything could be made so much simpler when it comes to creating advanced code). So, you're subject to the properties of PHP's syntax.

So when you add a function 'myfunction' to hook 'myhook', regardless of its scope, your function is still a function and therefore you still need to globalize the variables you want to be global.
That's my interpretation and it has been working so far. I know that hooks are not provided by PHP itself but from MyBB, but I don't need to globalize variables if they are "floating".
By floating hooks and function hooks you mean normal hooks (newthread_do_newthread_end) and hooks inside functions (datahandler_post_validate_post)?

I actually thought the opposite till half of your post Toungue
Yeah Toungue
(2014-04-05, 02:10 PM)Shade Wrote: [ -> ]That's my interpretation and it has been working so far. I know that hooks are not provided by PHP itself but from MyBB, but I don't need to globalize variables if they are "floating".

Quote:The scope of a variable is the context within which it is defined.

from http://www.php.net/manual/en/language.va....scope.php

Variables passed to PluginSystem::run_hooks by reference obviously don't need to be globalized inside a function but every other variable outside of the function would need to be.

Quote:[...]within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope.[...]
@Shade,

That's the same mistake you made in MyFBConnect. Toungue
Pages: 1 2