{"action":"created","issue":{"url":"https://api.github.com/repos/panique/huge/issues/585","labels_url":"https://api.github.com/repos/panique/huge/issues/585/labels{/name}","comments_url":"https://api.github.com/repos/panique/huge/issues/585/comments","events_url":"https://api.github.com/repos/panique/huge/issues/585/events","html_url":"https://github.com/panique/huge/issues/585","id":56131817,"number":585,"title":"[3.1][architecture] Should we avoid Singletons or not ?","user":{"login":"panique","id":156321,"avatar_url":"https://avatars.githubusercontent.com/u/156321?v=3","gravatar_id":"","url":"https://api.github.com/users/panique","html_url":"https://github.com/panique","followers_url":"https://api.github.com/users/panique/followers","following_url":"https://api.github.com/users/panique/following{/other_user}","gists_url":"https://api.github.com/users/panique/gists{/gist_id}","starred_url":"https://api.github.com/users/panique/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/panique/subscriptions","organizations_url":"https://api.github.com/users/panique/orgs","repos_url":"https://api.github.com/users/panique/repos","events_url":"https://api.github.com/users/panique/events{/privacy}","received_events_url":"https://api.github.com/users/panique/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-01-31T17:22:04Z","updated_at":"2015-02-01T03:29:16Z","closed_at":null,"body":"HUGE 3.0 has introduced several singletons, as this was by far the most simple way to implement certain things. The older 2.x versions used proper Dependency injection (for injecting db connections for example) for certain things, but it felt very complicated and bloated the code / architure quite much. This is a step backwards in architecture, but a step forward in usability. Weird situation. However, singletons are not nice, but have a look. For example the way the app gets the configs:\r\n\r\nhttps://github.com/panique/huge/blob/master/application/core/Config.php\r\n\r\n```php\r\n<?php\r\n\r\nclass Config\r\n{\r\n    private static $config;\r\n\r\n    public static function get($key)\r\n    {\r\n        if (!self::$config) {\r\n            self::$config = require('../application/config/config.' . Environment::get() . '.php');\r\n        }\r\n\r\n        return self::$config[$key];\r\n    }\r\n\r\n}\r\n\r\n```\r\n\r\nThis makes it easy to get any config by just doing `Config::get('xxx')` everywhere. But, it also makes testing harder (and hardcore developers angry), you probably know the discussion about the Singleton pattern. In this case the Config class has a global state, it only reads the config file the first time Config::get( ... ) is used.\r\n\r\n**My question:**\r\nWhat do you think ? Is it okay to leave it like that or should this project become cleaner, sticking to modern design patterns and provide a real, fully testable architecture ? Keep in mind that this is just a litte hobby project, not a funded-with-millions Symfony thing.\r\n\r\n**Interesting possible attempt on how to test this in phpunit**\r\nhttps://gist.github.com/cjsaylor/4131481\r\n\r\n*Side-note:* In v4.x the project will be built on top of another *real* framework."},"comment":{"url":"https://api.github.com/repos/panique/huge/issues/comments/72349619","html_url":"https://github.com/panique/huge/issues/585#issuecomment-72349619","issue_url":"https://api.github.com/repos/panique/huge/issues/585","id":72349619,"user":{"login":"normai","id":7976938,"avatar_url":"https://avatars.githubusercontent.com/u/7976938?v=3","gravatar_id":"","url":"https://api.github.com/users/normai","html_url":"https://github.com/normai","followers_url":"https://api.github.com/users/normai/followers","following_url":"https://api.github.com/users/normai/following{/other_user}","gists_url":"https://api.github.com/users/normai/gists{/gist_id}","starred_url":"https://api.github.com/users/normai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normai/subscriptions","organizations_url":"https://api.github.com/users/normai/orgs","repos_url":"https://api.github.com/users/normai/repos","events_url":"https://api.github.com/users/normai/events{/privacy}","received_events_url":"https://api.github.com/users/normai/received_events","type":"User","site_admin":false},"created_at":"2015-02-01T03:29:16Z","updated_at":"2015-02-01T03:29:16Z","body":"(1) One uneducated question please. Is above class Config code really a singleton? I cannot see any object creation involved, there is no new operator and no constructor, just the static $config variable conditionally filled via require. A singleton would return a object, not an array element. Hm .. perhaps in PHP the difference between object and array is blurred?\r\n\r\n(2) Use singletons? IMO sure! It is state of the art pattern for certain requirements. The fact, that it is harder to test seems secondary to me. A clean basic design is primary."}}