Removing Globals?

General discussion of development ideas and the approaches taken in the 3.x branch of phpBB. The current feature release of phpBB 3 is 3.3/Proteus.
Forum rules
Please do not post support questions regarding installing, updating, or upgrading phpBB 3.3.x. If you need support for phpBB 3.3.x please visit the 3.3.x Support Forum on phpbb.com.

If you have questions regarding writing extensions please post in Extension Writers Discussion to receive proper guidance from our staff and community.
Post Reply
rma-web
Registered User
Posts: 11
Joined: Sat Jan 16, 2010 1:27 am

Removing Globals?

Post by rma-web »

I don't know if Symfony 2 has something like the Zend_Registry class, but does anybody think that it might be better to store objects in something like Zend_Registry, or a phpbb equivalent, rather than just using
global $db, $template;

http://framework.zend.com/manual/en/zen ... using.html

igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Re: Removing Globals?

Post by igorw »

Dependency Injection. Symfony provides a DI container: http://components.symfony-project.org/d ... injection/

dams
Registered User
Posts: 6
Joined: Sun Oct 19, 2008 5:40 am

Re: Removing Globals?

Post by dams »

isnt that the same as useing globals ?

igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Re: Removing Globals?

Post by igorw »

No, it's the opposite. Instead of creating global instances and having your classes use those in a global scope you configure your container, telling it how to create classes and their dependencies. Then the container automatically passes the dependencies to the classes (they get injected).

Example: If a post_tools class depends on $db it takes it as an argument in the constructor. Like:

Code: Select all

class post_tools {
  public function __construct($db) {
    $this->db = $db;
  }
}
Then it just uses $this->db.

This way post_tools does not depend on any global state, nor on any kind of registry.

Post Reply