Variable(s) accessible to all objects

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Variable(s) accessible to all objects

Post by Ree »

I have an array of strings, which I want all of my objects to be able to use. One way to do that would be putting

Code: Select all

global $array_name;
in all of the methods that need to use it in all of the classes that have such methods. Another - passing the array itself to methods. Both ways look awful to me. Is there a nice and painless way to achieve the functionality I need?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you could grab them from the superglobals, but it's advised you pass the information in some fashion. Alternately, a singleton could work as well.. which is basically the same but the data is stored in an object instead.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Is it worth writing a class just for storing the array? Not sure, but I'll try that singleton thing.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Have a look at this:

Code: Select all

class LanguageData
{
  function LanguageData()
  {  
  }

  function getString($key)
  {
    include('en.php');
    return $lang[$key];
  }
}

class UserClass
{
  function UserClass()
  {
  }

  function needsLanguageInfo()
  {
    //code
    $string = LanguageData::getString(256);
  }
}
Why would I want to use singleton if I can simply access the language string I need via class::method()? It seems to solve my problem, but maybe singleton has advantages I'm not aware of?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your code will on each call load the file via the include. You may only want to load it once.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Now does that look like a possible singleton usage? It's just an example, of course.

Code: Select all

define('LANG', 'en.php');

class LanguageData
{
  var $lang;

  function LanguageData($file)
  {
    include($file);
  }

  function &Singleton($file)
  {
    static $object;
    if (!isset($object))
    {
      $object = new LanguageData($file);
    }
    return $object;
  }

  function getString($type, $number)
  {
    return $this->lang[$type][$number];
  }
}

class Test
{
  var $lang;

  function Test()
  {
    $this->lang = &LanguageData::Singleton(LANG);
  }

  function needsSomeString()
  {
    $string = $this->lang->getString('type', 5);
    //do stuff
  }
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes, that's a singleton roughly.. for php4 anyways ;)
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

roughly... that means incorrect in some ways? and yes, I use php4.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

no, perfectly fine for php4.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Basically, it's a slightly wizard-ish trick that doesn't bend any rules and is portable to php5 but don't use it too often or you may confuse people.
Post Reply