custom superglobals

php.internals

Ron Korving

21 years ago
Hi, Wouldn't it be a good feature to have users create their own superglobals? I think it would be good for at least two reasons (rather similar though): - Application settings can be stored in such a global, so that objects can use them without needing the information passed to functions or using the rather ugly "global" statement. - A singleton constructor could use it to grab information. Normally, in my own database abstraction layer, I would create the database object and pass the constructor information on where to connect. I don't want to implement my DB class to open a file to read connection information and I don't want it to use the "global" statement. This presents a problem if I want it to be a singleton object, because I'd need the information everytime I call getInstance() which is of course not really an option. Personally, I don't consider using a superglobal from within the constructor an ugly practice. Ron

Sean Coates

21 years ago
Ron Korving wrote:
> Wouldn't it be a good feature to have users create their own superglobals?
I realize that you're probably talking about creating custom superglobals in the PHP core, but you might want to look at Sara's Runkit project (in PECL). It allows custom superglobals. http://pecl.php.net/runkit (beware the segfaults (-; ) S

Rasmus Lerdorf

21 years ago
Ron Korving wrote:
> Hi, > > Wouldn't it be a good feature to have users create their own superglobals? I > think it would be good for at least two reasons (rather similar though): > > - Application settings can be stored in such a global, so that objects can > use them without needing the information passed to functions or using the > rather ugly "global" statement. > > - A singleton constructor could use it to grab information. Normally, in my > own database abstraction layer, I would create the database object and pass > the constructor information on where to connect. I don't want to implement > my DB class to open a file to read connection information and I don't want > it to use the "global" statement. This presents a problem if I want it to be > a singleton object, because I'd need the information everytime I call > getInstance() which is of course not really an option. Personally, I don't > consider using a superglobal from within the constructor an ugly practice.
That's what $GLOBALS is for. -Rasmus