wepnop Posted February 15, 2011 Share Posted February 15, 2011 Im planning to add some extra variables that need to be global. I have a singleton that encapsulate the database link: # SINGLETON class RegistroGlobal { private static $_instance; var $db; public static function GetInstance() { if (!self::$_instance instanceof self) { self::$_instance = new self(); } return self::$_instance; } # You can suppress the error message on failure by prepending a @ to the function name. private function __construct($RutaIconos='') { $this->db = conectar_bd(); $this->RutaIconos = $RutaIconos; // put normal constructor code. // it will only ever be called once } public function checkDbCon() { if ($this->db) { return true; } else { return false; } } public function getDB() { return $this->db; } public function getRutaIconos() { return $this->RutaIconos; } } I was thinking in using a hash into the singleton for configuration values, so you can easily add anything to it just getting or setting the hash, and you dont need to modify the singleton or nothing. Also my method names are ok? they are Java style, there are something to improve in this code, in general? Quote Link to comment https://forums.phpfreaks.com/topic/227720-global-variables-and-singleton/ Share on other sites More sharing options...
trq Posted February 15, 2011 Share Posted February 15, 2011 The name of your class doesn't seem to match your methods. What exactly is this thing meant to be? Quote Link to comment https://forums.phpfreaks.com/topic/227720-global-variables-and-singleton/#findComment-1174470 Share on other sites More sharing options...
wepnop Posted February 15, 2011 Author Share Posted February 15, 2011 A singleton class that contains some global variables that are required in a lot of scripts: db link, config, paths, etc Quote Link to comment https://forums.phpfreaks.com/topic/227720-global-variables-and-singleton/#findComment-1174551 Share on other sites More sharing options...
trq Posted February 15, 2011 Share Posted February 15, 2011 So.... a registry. Hard coding methods like getDB() isn't very flexible. Take a look at overloading. http://php.net/manual/en/language.oop5.overloading.php Quote Link to comment https://forums.phpfreaks.com/topic/227720-global-variables-and-singleton/#findComment-1174719 Share on other sites More sharing options...
Zyx Posted February 16, 2011 Share Posted February 16, 2011 Singletons are evil. Registries are evil. Global variables are evil. Static class variables are evil. Do not use them. EVER. Why? They introduce a global state which is hard to track, hard to test and hard to debug. Once your application gets bigger, you will get lost in the number of dependencies and the way the application works. Every change in component A will destroy component B without no visible reason. If you want to improve it, take a look at the dependency injection container pattern, and the dependency injection idea itself. You do not even have to implement it on your own, because there are existing, standalone implementations, such as the Symfony 2 Dependency Injection Component ( http://components.symfony-project.org ) Quote Link to comment https://forums.phpfreaks.com/topic/227720-global-variables-and-singleton/#findComment-1174874 Share on other sites More sharing options...
wepnop Posted February 17, 2011 Author Share Posted February 17, 2011 nooooooooooooooo, i was thinking in mysef like a pro for using a singleton... xDDDDDDDDD Anyway i didnt understand very well what that pattern does, anyway im not in a pure OO php for now, so.... They introduce a global state which is hard to track, hard to test and hard to debug. Once your application gets bigger, you will get lost in the number of dependencies and the way the application works. Every change in component A will destroy component B without no visible reason. Thats why i wanted to add a hash inside. With that i can add any new variables dinamycally without a problem, and just use the ones i trully need. I suppose that it will be nice to use a system where if i use the error system for example, it adds to the singleton definition the variables it needs automatically, but i dont know how i can do that. In ruby it will be easy just aliasing the singleton construct method, for example. Maybe writing the config array in a file? For now the singleton is not that evil. Every entry is independent so... it dont destroy nothing. Finally, the application isnt going to grow a lot. Is a part of a set of functions that i use for small php projects, schoolworks, fun... etc, Anyway that pattern seems interesting and i wll take a look for it, i like writing such systems on my own for learning... i dont want to use a framework for now because i dont need the speed they give to you (im not getting paid for my actual works...) Quote Link to comment https://forums.phpfreaks.com/topic/227720-global-variables-and-singleton/#findComment-1175543 Share on other sites More sharing options...
wepnop Posted February 18, 2011 Author Share Posted February 18, 2011 I readed more about the pattern and i just dont know why thats better for me. Well, i see that is a some form of a factory/composite method that remove dependencies so each person or program can have any other modules... Singleton is nice for my system and aspirations, and i dont need anything more that the config file and expandable hash because they dont use differents implementations or methods at all, or share with other systems... Now my problem is with loading the singleton each time, reading the parsed config file and loading less or more variables... im thinking in speed that process. Quote Link to comment https://forums.phpfreaks.com/topic/227720-global-variables-and-singleton/#findComment-1176120 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.