Plugin Development, scope, & $GLOBALS

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tgreer
    Member
    • Oct 2005
    • 50

    Plugin Development, scope, & $GLOBALS

    One of my recurring challenges with plugin development, is scoping variables.

    If I have a plugin that creates variables, and want to use those variables in a variety of templates, sometimes those templates are in-scope, sometimes they are called via functions and so are out of scope.

    My work-around is to use the $GLOBALS[] array to reference my variables.

    I'd like to see some discussion on the pros and cons of that, and other approaches.

    For example, what are the ramifications of storing my variables in the vBulletin $show array?

    Another alternative is to add a plugin to each hook location where I want my variables, with "global $myvar" to force global scope.

    I know this type of discussion is usually handled at vbulletin.org, but I find the community there less than helpful/mature/professional, so thought I'd try starting the discussion here.
    Thomas D. Greer
    www.tgreer.com/printforum
  • Scott MacVicar
    Former vBulletin Developer
    • Dec 2000
    • 13286

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

    My Blog | Twitter

    Comment

    • tgreer
      Member
      • Oct 2005
      • 50

      #3
      Not sure what you mean by "create your own registry". Do you mean array? And would that work within a template? Sorry, but your answer creates more questions than it answers, for me.

      1) I have a plugin, which is of course PHP code. I hook it at global_start, so the variables have global scope. Let's call these:

      Code:
      $myGlobalVar_1 = "test1";
      $myGlobalVar_2 = "test2";
      2) I wish to use these variables in a template. This template is used within a function (such as the postbit factory, for example). In order to use them I either have to

      2a. Create another plugin, say at postbit_start and explicitly scope these global, so the function can use them

      Code:
      global $myGlobalVar_1,$myGlobalVar_2;
      I understand that fully, and use it. However, sometimes I wish to use those variables in multiple templates. I could create a new plugin just like the one above for every single hook location I need, OR:

      2b. I can refer to the variables by using $GLOBALS inside the template:

      Code:
      $GLOBALS[myGlobalVar_1]
      My question is, is there a better way than using $GLOBALS?

      Your response

      Why not create your own registry for variables and use $vbulletin->myvars["moo"]
      Q. What do you mean by "create your own registry". Do you mean, create my own array?

      Q. How would I associate my array/variable with the $vbulletin object?

      Q. What is the syntax for using the $vbulletin object inside of a template?
      Last edited by tgreer; Thu 6 Apr '06, 11:31am.
      Thomas D. Greer
      www.tgreer.com/printforum

      Comment

      • tgreer
        Member
        • Oct 2005
        • 50

        #4
        Or, by "registry" to you mean "class/object"?

        Code:
        $myObj =& new MyClass;
        $myObj->property1 = 'some value';
        $myObj->property2 = 'some other value';
        $vbulletin->myObj = $myObj;
        I understand that would be good approach for large plugins. However, it still doesn't answer how you'd use $vbulletin->myObj['property1'] inside of a template.
        Thomas D. Greer
        www.tgreer.com/printforum

        Comment

        widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
        Working...