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
Removing Globals?
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.
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.
Re: Removing Globals?
Dependency Injection. Symfony provides a DI container: http://components.symfony-project.org/d ... injection/
Re: Removing Globals?
isnt that the same as useing globals ?
Re: Removing Globals?
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:
Then it just uses $this->db.
This way post_tools does not depend on any global state, nor on any kind of registry.
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;
}
}
This way post_tools does not depend on any global state, nor on any kind of registry.