vBulletin

Thank you for visiting. This is our website archive. Please visit our main website by clicking the banner above.
vBulletin FAQ is dedicated to helping the forum owner build, manage and profit from his vBulletin Forum
vBulletin Web Hosting - Free skins and styles for your vBulletin - Search Engine Optimization




Managing Variables

Coder1
04-05-2006, 08:07 PM
I hope this is the correct place to discuss plug-in/hack development issues in general.

I find one my biggest challenges is in scoping variables. If your code can run all in one location, that's fine... simply add your plugin to the appropriate hook location, and then use any variables your code defines in the appropriate template.

However, when your variables might be used in several templates, how do you declare them and access them?

For an example, my "invite" plugin. If you don't know what it is, search MSN for "PostScript Variable Data Printing". The first result takes you to a thread on my site, and if you scroll down to the bottom, you'll see an invitation to join. The text contains the search engine and the search terms you used.

The main bulk of that code runs at global_start, and defines a few variables, including $search_engine and $search_terms.

However, I use these variables in the forumrules template. Since that template is called within a function, I would have to have another plugin that performed "global" declarations for those variables, and add them to a hook location within that particular function. If I wanted to use my variables in yet other templates, I'd end up with several plugins just to scope these variables!

Instead, I use the $GLOBALS array, and reference the variables as $GLOBALS[search_engine] and $GLOBALS[search_terms].

There has got to be a better way. How do YOU do it?

Peggy
04-05-2006, 09:01 PM
this is all very interesting to me, altho a coder, I will never be, for very obvious reasons ;)

noppid
04-05-2006, 09:03 PM
Having been faced with the same dilema, I've often resorted to haking the function rathen then use $GLOBALS. But of course, I had no intention of the code being seen in public. I also have a reasonable system to manage hacked files. I think I have only one though on 3.5.4.

So anyway, one solution is to hook into an existing array that may be passed to a function rather then use $GLOBALS.

Coder1
04-06-2006, 06:11 AM
I tried that, too, for example, storing all of my variables in the $show[] array. That worked, but caused some unexpected results. It seemed that I was overwriting or "pushing out" other show entries. Is that possible? Specifically, if I stored my variables in $show[], then showthread would think my browser was non-popup, so the thread controls would be misplaced. Once I started using $GLOBALS instead, the problem went away.

The problem with $GLOBALS, though, is that not every web host will have register_globals turned on.

So, by hacking the files, you mean you physically edit the PHP functions, instead of using a hook? Yeah - that wouldn't fly for a released plug-in!

What's really needed is a mechanism to have code run at multiple hook locations, without having to define multiple plug-ins that simply repeat the same declarations.

Coder1
04-06-2006, 12:14 PM
I posted a similar discussion on www.vbulletin.com (http://www.vbulletin.com), and got this reply:


Why not create your own registry for variables and use $vbulletin->myvars["moo"]


Can anyone shed light on this? What is meant by "create your own registry for variables"? I think maybe that's a typo, and he meant, create your own ARRAY to store your variables.

Ok, that's well and good, but the next part baffles me. How do my arrays/variables become part of the $vbulletin object?

Also, since templates are HTML, not PHP, could you even reference the $vbulletin object inside of a template?

That answer just generated more questions than it answered, I'm afraid.

OR - by "registry" does he mean "object"? So, instead of simply creating raw PHP variables, the plugin would define a class, the variables would be properties, the class would be instantiated as an object, and the object added to $vbulletin?


$myObj =& new MyClass;
$myObj->property1 = 'some value';
$myObj->property2 = 'some other value';
$vbulletin->myObj=$myObj;


Ok... I can see why that would be a good approach for very complex plugins, but that still doesn't answer how you'd use $myObj within a template.

noppid
04-06-2006, 03:20 PM
I posted a similar discussion on www.vbulletin.com (http://www.vbulletin.com), and got this reply:

Can anyone shed light on this? What is meant by "create your own registry for variables"? I think maybe that's a typo, and he meant, create your own ARRAY to store your variables.

Ok, that's well and good, but the next part baffles me. How do my arrays/variables become part of the $vbulletin object?

Also, since templates are HTML, not PHP, could you even reference the $vbulletin object inside of a template?

That answer just generated more questions than it answered, I'm afraid.

OR - by "registry" does he mean "object"? So, instead of simply creating raw PHP variables, the plugin would define a class, the variables would be properties, the class would be instantiated as an object, and the object added to $vbulletin?


$myObj =& new MyClass;
$myObj->property1 = 'some value';
$myObj->property2 = 'some other value';
$vbulletin->myObj=$myObj;


Ok... I can see why that would be a good approach for very complex plugins, but that still doesn't answer how you'd use $myObj within a template.

vBulletin imports and uses XML. There may be a way to put an xml file some where and the contents added to the vbulletin object. Then of course there is template access. It seems that there is a mechanisim that puts parts of the vBulltin object in the vboptions and bbuserinfo arrays for use in templates. This is something we can figure out by reading global.php and init.php easily I would think. It may even be documented if such an xml include is possible.

Of course these are ifs. I've never investigated this concept. But I'm interested and will certainly take a look at docs and files for clues later. You may be onto something.


vBulletin

seo book

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum