Programming should be object oriented. This is a message we get from everywhere nowadays. Back ind the old days, PHP scripts were looong series of includes, functions and global variables. Not anymore. And we should be grateful, because the object oriented way of doing things, really has improved our code, and made it easier to understand and extend. However, there are a few scenarios where I miss the cold old call of a global function that does some kind of minimal functionality. Convenience functions. But is the usage of convenience functions still legitimate today? Is it OK to define a random function somewhere global, and just use it here and there, across your project? I would say: It depends! Let me show you an example where I think, even today, that it’s perfectly reasonable and fine to use global functions.
The MySQL timestamp
Creating timestamps for your MySQL queries with PHP involves the use of the date() and time() functions. If you wish to add the current time to a MySQL query, you need to do something like date(‘Y-m-d’, time()). And how often do you need this? I found to use this type of code line quite often, so I have allowed myself to create a global function called timestamp() which I can call everywhere.
function timestamp($time = false) { if(!$time) { $time = time(); } return date('Y-m-d H:i:s', $time); }
This type of functions is something that I still consider legitimate to use as custom defined global function, because it is used across the entire project, many different places, is is similar to core PHP functions like time() and date().
6 comments (leave a comment)
I agree that functions like this are very useful… though I bundle them into library objects and access them using
dates::mysql();
or something. This helps organise all your useful (and previously global) functions into libraries.Some of mine are: arrays, dates, debug, files and html.
Reply by Jakob on January 30, 2011 at 19:22
Great point! That way you can maintain an ordered and elegant collection of functions, that can also easily be integrated in multiple projects!
by Rick Hambrook on January 28, 2011 at 03:40
I would be inclined to disagree with your example although I agree completely that using globally available functions is still useful.
I would have said that such a specific function such as the one you have would go inside the class the manages the database connection. For example if I was using MySQLi or PDO I would extend it and add another method that would handle what you have said for example
class customMySQLi extends mysqli {
public function timestamp($time = null) {
return date(‘Y-m-d H:i:s’, $time);
}
}
This is because if you changed your database engine the format of the other engines natural date format may be stored differently (even MySQL has at least 2 date formats).
Also as an alternative you could create static methods on in a class, which would also be globally available throughout your application but helping to keep your global namespace clear (think of your include of functions.php as a class instead)
by Matt on February 2, 2011 at 09:39
Great Post. Thanks!
by Sarah on February 6, 2011 at 04:44
thnx bro
by facebook layouts on February 15, 2011 at 15:31
your good
by kourtnie on February 19, 2011 at 19:08