vBulletin Modifications

Plugins, Variables, and scope issues.

Welcome to vBHackers.com! - vBHackers Updates:

Go Back   vBulletin Modifications > General vBulletin Section > General vBulletin Support

Reply
 
LinkBack Thread Tools
Old 04-06-2006, 07:13 PM   #1
tgreer
Guest

Default Plugins, Variables, and scope issues.

Hello, my first post here. I'm a refugee from vbulletin.org - I just couldn't stand the atmosphere there. Onto my first question.

Often, the purpose of a plugin is essentially to create a variable that can be used in a specific template. All well and good. However, many templates are fetched by a function, so there are often scope issues.

To resolve this, I'll often create two plugins: the main PHP code that does the queries, etc. and builds the variable(s) I'll use in the template. Depending on the function, it'll be hooked where it makes the most sense. For example, one of my plugins parses the HTTP_REFERER, so is hooked at global_start. Then, I'll create another plugin that merely globalizes the variable(s), and is hooked within the function that calls the template.

What, though, if you want to use the variables within multiple templates? One answer is to make a corresponding number of "globalizer" plugins. Yuck.

Instead, I'll refer to my variables using the $GLOBALS array:

$GLOBALS[my_variable]

Finally, my question: is that the best way? Is there some other mechanism for building variables that can be used within templates, which avoids the function-scope issue?

One terse reply I get said to use the $vbulletin object. Ok - how? And assuming it's as simple as

You must first be a registered member to view any code.
then, how do you reference $vbulletin->my_variable from within a template?




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Old 04-06-2006, 07:40 PM   #2
vBulletin Guru
Arnoud's Avatar
Join Date: Nov 2004
Real Name: Arnoud Kuipers
Location: Europe, Flanders
Arnoud is on a distinguished road

Default

Firstly: welcome to the site !

In reply to your $vbulletin-> question:
The $vbulletin-><array> works like this (I'll take the example of userinfo here):

If you wish to display information from the user currently viewing the file (note: this only works in files!) you'd have to do it like this:
You must first be a registered member to view any code.
while in templates you'd have to use this:
You must first be a registered member to view any code.
So this will most likely be the case for any of the custom variables you make. You can use the $vbulletin->xyz[zyx] variables in templates by simply defining them as normal variables in a plugin, though. Basicly you have to turn the code you posted around in order to make it work with templates:

You must first be a registered member to view any code.
And then use $myvariable in your templates.

In reply to the first bit of your post:
If you wish to use a variable site-wide you're best off putting it in one of the global hooks, as this file is called for in every single vB file (custom or not) in your forum root dir (I guess you do realise it is not a good idea to stick a couple of queries in there, heh ;)). Depending on where you use the variables you might need to redefine them in more specific plugins ofcourse.

I must say I haven't worked with the plugin system a lot so I don't really know the exact details and thus my post might be a bit vague. Please do not hesitate to ask if you don't understand what I've posted above or if it doesn't answer your question(s), and I'll gladly elaborate and if needed do some research of my own for you ;).
__________________
Need some custom work done for your site, but can't find a (freelance) coder willing to take on the job? Good news: you needn't look further, as I'm currently accepting custom jobs! From a template fix to a completely new portal system, you name it and I'll make it exclusively for your site! Interested? Send me a PM for details. ;)


Please note that I will only provide support through the vBHackers.com Forums, not through MSN/email/PM.




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 04-06-2006, 08:05 PM   #3
tgreer
Guest

Default

Hmm. The problem is basically PHP scope. All variables referenced within a function are locally scoped, unless they are referenced with the "global" modifier. Right?

If you have a plugin that runs PHP code at global_start, that's all fine.

As soon as you try to use a variable created by your plugin, within a template, which is in turn fetched from within a function, you "lose" your variable. Make sense so far?

SO, you can either add ANOTHER plugin, assign it to a hook that is inside the function which calls the template, and there add "global $myVar", OR, use $GLOBALS[myVar] in your template. Right?

Instead, I'd like to store my variables inside an object which is ALREADY globalized across the board, and I'm being told that "$vbulletin" is that object.

I really don't think your approach would work.

You must first be a registered member to view any code.
would create a variable $myvariable, which is locally scoped. That "local" may or may not be "global" depending on where the plugin runs, but if it doesn't run precisely where you're going to use it, then you're faced with the exact same scope issue.

Last edited by tgreer; 04-06-2006 at 09:26 PM..




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 04-06-2006, 08:16 PM   #4
vBulletin Guru
Arnoud's Avatar
Join Date: Nov 2004
Real Name: Arnoud Kuipers
Location: Europe, Flanders
Arnoud is on a distinguished road

Default

Ah, now I get what you're trying to do ;).

The problem with "$vbulletin->xyz[yxz]" is that this does not work in templates, so you'll still end up redefining this every time you need it in a function.

I don't think I've ever seen a hack which needs to define its variables across multiple non-custom files. May I ask what you're making ?

Also, you can give the $vboptions variable a shot. These are basicly the vBulletin options, and can be called for in files like this: $vbulletin->options[xyz], and in templates: $vboptions[xyz]. These are globalized in most functions, but not in all of them.




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 04-06-2006, 08:51 PM   #5
tgreer
Guest

Default

I'm not making anything, currently. But all four of my own custom plugins have faced this scope issue:

1) The "invite" plugin. It parses HTTP_REFERER, so needs to run at global_start to collect the search engine and search terms, which are stored in variables. However, I use these within the forumrules template, which is scoped to a function.

2) I use a private section of my forum to handle client projects. Each client has a sub-forum. I've written a billing system, and log my hours into it. Each post in this category has an option to attach a billing code, and it automatically gives them a running total on each of my posts. When I close a thread, they get an invoice, automatically, as the last post in the thread and also via email. Some of my variables are used on FORUMDISPLAY, and yet others are used inside postbit, which is scoped to the postbit_factory function.

3) Analysis & Opinion. If you go to my site, http://www.tgreer.com/cmps_index.php , click the blue navigational buttons along the top. See how each category lists the sub-forums, and also lists any associated articles? Lots of code there, spread across lots of functions. So my variables are always out of scope.

4) phpAdsNew integration. Currently this is turned off, but I run the invocation code/plugin at global_start, but display the ads in showthread, which means the showthread template... but I had to add another plugin at postbit_display_start to globalize the ad variable.

It's just a mess, and I know there has to be a better mechanism than this. I just can't find any documentation/discussion.




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 04-07-2006, 08:56 AM   #6
vBulletin Guru
Ken Iovino's Avatar
Join Date: Mar 2004
Real Name: Ken Iovino
Location: Miami, Florida
Ken Iovino is on a distinguished road

Default

I never acually looked deep into this, but I ran into this problem aswell here on this board. Quite a few variables of mine got lost in the scoop and I was forced to use $GLOBALS[my_variable]. Which in my mind, doesn't look to good and can confused me when re-editing templates.

I'm using a simple if() check to see if the user viewing the hack has clicked installed. In order for me to do this, I have to run a query to see if the user has clicked install or not. That will return my $var true or false. Though the $var will get lost in the scoop since I have to use this variable in the postbit template, and not the showthread template. And the query is being executed in the start of the showthread page.

So I'm stuck with using... <if condition="$GLOBALS['installcheck']">yes<else />No</if>.
__________________
Ken Iovino / Escalate Media




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 04-07-2006, 09:09 AM   #7
vBulletin Guru
Arnoud's Avatar
Join Date: Nov 2004
Real Name: Arnoud Kuipers
Location: Europe, Flanders
Arnoud is on a distinguished road

Default

I think those functions did make hacking vB harder, even though they were ment to make it easier :/.




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -3. The time now is 07:47 PM.

Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc. (Patent Pending)