I have asked aqbout timezones and date/time before but this is a more specific question, more about Objects in PHP.
<?PHP
//set the user's time zone on page load
date_default_timezone_set("America/Los_Angeles");
//convert the timestamp from DB into the user's time
$timestamp = '2008-05-01 13:44:19'; //this would normally be returned from mysql
echo ConvertDateTime($timestamp, "America/Los_Angeles");
//here is the function to convert it
function ConvertDateTime($timeString, $timeZone)
{
if ( $d = new DateTime($timeString) ) {
$d->setTimeZone(new DateTimeZone($timeZone));
return $d->format("Y-m-d H:i:s");
}
return null;
}
?>
Ok this is probably a dumb queston to some but I really do not know a lot or have much experience when it comes to classes and objects, generally on a page for example I will create 1 database object and run every mysql query on the page with that 1 object set. I always think less objects is probably better but I have read on this site that having hundreds of objects would be the same speed and resourses used compared to 1 object, not sure f it’s true or not.
My question, in the function above, you can see that a new DateTime object is created everytime the function is called, could be 100 times on some pages in my situation. So should I use like a singlton or even I could set a dattim object 1 time in the header where my database object is set as well or should I just let the function create a ton of object?
Please explain the pros and cons and which I should probably do, appreciate any advice, some may say I am pre-optimizing, well I have been optimizing a current site for over 2 years to get it where I wanted it and now I am re-building that SAME site so I kinda know what userload to expect and stuff and I would rather fix something like this now instead of later.
Singleton are not without problems — for instance, they often make (automated) testing difficult. Which is one of the reasons for which they are less and liked by “big” Frameworks (I’m thinking about Zend Framework and Symfony, which are reducing the number of Singletons for their next major version)
Using a global variable / object can go with side-effects, too ; If a variable is meant as “local”, it should be local — code readibily and ability to understand it are generally more important than a fraction of a millisecond.
For extreme and specific cases, the only (true) solution is to profile : you want your script / application to go faster ? To use less CPU ?
Then, first of all, profile it ! It’ll help identify bottlenecks — and show you on what you should concentrate.
Chances are you have plenty of things that could be optimized fastest (i.e. which would take less time to think about, code, and test) — and would get you higher performance gains.
In the end, you always have to choose between :
And, quite often, a new, more powerful server (or a second one) will finally coest less than a couple of weeks spent optimizing to get only 5% !
(Note I didn’t say you shouldn’t optimize ; but you shouldn’t over-optimize.)