Jump to content

Custom Superglobals


neoform

Recommended Posts

Don't be lazy when it comes to programming. This will you will start programming with short_tags enabled, register_globals and who knows what other bad-practice settings. You want to make your scripts as clean, readable and system-compatible as possible.

If the variables are something that should be global, declare them as global at the start of your functions. If it's not something that you function needs but it's not something very important (by something important I mean stuff such as a DB connection, some kind of setting etc'), simply pass it as a parameter to the function.

Of course, if these variables have a constant value, define them as constants with define().

 

Orio.

Link to comment
Share on other sites

Err, there's nothing wrong with using short_tags.. it's not bad code ethic since i've never seen a machine with php5 that doesn't have it enabled.

 

I use $GLOBALS right now to store a number of objects like my $GLOBALS['sql'] object that contains the mysqli object or $GLOBALS['tpl'] which is template object, both of which get used in pretty much every page on the site, this is exactly what superglobals were meant for, I just find it an ugly var name and would rather using something like $_SQL or $_TPL. Either way, using superglobals like this is not a bad practice.. I find "global $varname" to be far less clean.

Link to comment
Share on other sites

Using short_tags or register_globals is bad practice. There are many machines that have them disabled, and it's not so difficult to write "<?php" instead of "<?" and "<?php echo" instead of "<?=".

Except that, you are always in a risk of causing problems with xml (that uses <?xml).

 

If you are looking for something "clean", define these important variables in an array called $_SQL or $_TPL or whatever, and at the start of every function use global on them. I did that alot in projects I have done before- it's clean, clear and easy to use.

 

Orio.

Link to comment
Share on other sites

If you are looking for something "clean", define these important variables in an array called $_SQL or $_TPL or whatever, and at the start of every function use global on them. I did that alot in projects I have done before- it's clean, clear and easy to use.

 

Isn't that what I said?

Link to comment
Share on other sites

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.