which is more expensive: function call or retrieve from $_SESSION?

To all the PHP gurus. . .

Which method is expensive - function call or retrieve from $_SESSION?

I am asking this question from a performance and resource usage view point.

ex: today's date may be retrieved and stored in a var

$today = date("Ymd");

For most webapps, this var is only reused within a controller or a class, at best.

But, how about expanding the scope to the whole app? (and thus the reason for this question)

Would it be better to init a session var with the date in an entry controller/class, then retrieve the session var for each class or controller that needs it?

slight tangent/extra credit: ;)

what about using a DB to save/retrieve session info; does the scale tilt the other way?

TIA for all comments. . .

Hi paul77.

To have positive effect of this changes, you should at first ask yourself: “Whether my application uses date(“Ymd”) widely?”. If you answer “Yes”, then maybe the following points will be of assistance for you. Otherwise (answer is “No”), you may forget about this little feature :)

  1. Function call obviously is more expensive then retrieing from variable (as for variable from $_SESSION array).

  2. In my opinion, storing of date("Ymd") this way could slightly increase the performance (depends on how often you have to use this date).

  3. Using of DB for this purpose would reasonable if you have some kind of settings_table for storing some configuration or other information. Thus, if you need to request this table you can also add date_field to your SELECT and you don't need to request DB separately.

I wouldn’t suggest to query DB for this date separately. This way, I think, date(“Ymd”) would be a little faster :)

Quote

Hi paul77.

To have positive effect of this changes, you should at first ask yourself: “Whether my application uses date(“Ymd”) widely?”. If you answer “Yes”, then maybe the following points will be of assistance for you. Otherwise (answer is “No”), you may forget about this little feature :)

  1. Function call obviously is more expensive then retrieing from variable (as for variable from $_SESSION array).

  2. In my opinion, storing of date("Ymd") this way could slightly increase the performance (depends on how often you have to use this date).

  3. Using of DB for this purpose would reasonable if you have some kind of settings_table for storing some configuration or other information. Thus, if you need to request this table you can also add date_field to your SELECT and you don't need to request DB separately.

I wouldn’t suggest to query DB for this date separately. This way, I think, date(“Ymd”) would be a little faster :)

I appreciate your comment. 

I thought as much regarding the expense of using a function call vs. retrieving var data.  It’s good to have validation by another as well. :P

Some apps that I've built utilize some data across multiple classes using the session.  This question was posed to find out if it makes sense for even the trivial functions in php.

-Paul