Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/227720-global-variables-and-singleton/
Share on other sites

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 )

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...)

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.