SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Using objects as variable storage receptacles

    It's fairly common practice in Javascript to use objects for storing variables in order to avoid using global variables. Mostly this is to avoid conflicts with other scripts, and for neatness' sake. I have little experience with OOP in PHP so I wonder if this is also the case, if it's common practice. I find myself doing things like this increasingly so in functions:
    PHP Code:
    global $fish$dog$cat$gerbil$beetle$rat
    I don't like it. I can see that my script would benefit from using a more object oriented approach and I intend to change it to that eventually, but for now I wonder if using objects as containers to store variables instead of using global is a good idea.

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    I intend to change it to that eventually, but for now I wonder if using objects as containers to store variables instead of using global is a good idea.
    Yes, that's to a great extend the purpose of objects.

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Excellent, tusind tak.

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    So, if I just want to use an object as a global storage receptacle for a bunch of variables I wonder whether I should do this:
    PHP Code:
    class {
      public static 
    $foo 'foo';
      public static 
    $bar 'bar';
    }

    function 
    doStuff() {
      echo 
    A::$foo;
    }

    function 
    doOtherStuff() {
      echo 
    A::$bar;

    Eventually I'll make those functions become methods of a class (or classes) but for now I wonder if this is all right.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •