Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Personally, I would either write module that abstracts away the need for a global variable, or at least has a getter function for retrieving it. Something like

function foo_get_the_service ()
{
  static $service;

  if (! isset($service)) {
    $service = foo_init_the_service();
  }

  return $service;
}

Several Drupal API functions do something similar (eg, menu_get_active_trail() and menu_set_active_trail());

And then preferably, your module would also have helper functions so you don't need to access the global directly. If something really does need the global, then you would use the getter.

If your global is an instance of a proper PHP class, then you could look into using a real singletonsingleton.

Personally, I would either write module that abstracts away the need for a global variable, or at least has a getter function for retrieving it. Something like

function foo_get_the_service ()
{
  static $service;

  if (! isset($service)) {
    $service = foo_init_the_service();
  }

  return $service;
}

Several Drupal API functions do something similar (eg, menu_get_active_trail() and menu_set_active_trail());

And then preferably, your module would also have helper functions so you don't need to access the global directly. If something really does need the global, then you would use the getter.

If your global is an instance of a proper PHP class, then you could look into using a real singleton.

Personally, I would either write module that abstracts away the need for a global variable, or at least has a getter function for retrieving it. Something like

function foo_get_the_service ()
{
  static $service;

  if (! isset($service)) {
    $service = foo_init_the_service();
  }

  return $service;
}

Several Drupal API functions do something similar (eg, menu_get_active_trail() and menu_set_active_trail());

And then preferably, your module would also have helper functions so you don't need to access the global directly. If something really does need the global, then you would use the getter.

If your global is an instance of a proper PHP class, then you could look into using a real singleton.

New singletone link.
Source Link
mpdonadio
  • 38.1k
  • 8
  • 89
  • 142

Personally, I would either write module that abstracts away the need for a global variable, or at least has a getter function for retrieving it. Something like

function foo_get_the_service ()
{
  static $service;

  if (! isset($service)) {
    $service = foo_init_the_service();
  }

  return $service;
}

Several Drupal API functions do something similar (eg, menu_get_active_trail() and menu_set_active_trail());

And then preferably, your module would also have helper functions so you don't need to access the global directly. If something really does need the global, then you would use the getter.

If your global is an instance of a proper PHP class, then you could look into using a real singletonsingleton.

Personally, I would either write module that abstracts away the need for a global variable, or at least has a getter function for retrieving it. Something like

function foo_get_the_service ()
{
  static $service;

  if (! isset($service)) {
    $service = foo_init_the_service();
  }

  return $service;
}

Several Drupal API functions do something similar (eg, menu_get_active_trail() and menu_set_active_trail());

And then preferably, your module would also have helper functions so you don't need to access the global directly. If something really does need the global, then you would use the getter.

If your global is an instance of a proper PHP class, then you could look into using a real singleton.

Personally, I would either write module that abstracts away the need for a global variable, or at least has a getter function for retrieving it. Something like

function foo_get_the_service ()
{
  static $service;

  if (! isset($service)) {
    $service = foo_init_the_service();
  }

  return $service;
}

Several Drupal API functions do something similar (eg, menu_get_active_trail() and menu_set_active_trail());

And then preferably, your module would also have helper functions so you don't need to access the global directly. If something really does need the global, then you would use the getter.

If your global is an instance of a proper PHP class, then you could look into using a real singleton.

Source Link
mpdonadio
  • 38.1k
  • 8
  • 89
  • 142

Personally, I would either write module that abstracts away the need for a global variable, or at least has a getter function for retrieving it. Something like

function foo_get_the_service ()
{
  static $service;

  if (! isset($service)) {
    $service = foo_init_the_service();
  }

  return $service;
}

Several Drupal API functions do something similar (eg, menu_get_active_trail() and menu_set_active_trail());

And then preferably, your module would also have helper functions so you don't need to access the global directly. If something really does need the global, then you would use the getter.

If your global is an instance of a proper PHP class, then you could look into using a real singleton.