NYCPHP Meetup

[nycphp-talk] define variables versus assigned variables

Eddie Drapkin oorza2k5 at gmail.com
Sun Jan 3 02:25:22 EST 2010


The primary difference between a variable and a constant is
mutability.  If your database authorization details are not going to
change, make them constants.  If your database details DO change
throughout the execution of a script, make them variables.  In regards
to the performance of constants in PHP, that's an incredibly minor
improvement (a microoptimization, really) and it's my opinion that you
ought to be writing software to be good code and not have to hack
around considerations like how many microseconds your database
username declaration takes.

My advice: use constants for script immutable values and variables for
mutable values.  If you want to have a class constant, do that, but
it's not worth the hassle to write a class around the idea that class
constants are faster than global constants.  Use which tool is right
for the job.  Depending on the structure of your application,
DatabaseClass::USERNAME might make more sense than DB_USERNAME, but
that decision ought to be dictated by the structure of your code, not
some small performance consideration.

-Eddie

On Sun, Jan 3, 2010 at 12:52 AM,  <webmaster at vbplusme.com> wrote:
> Hello Konstantin,
>
> Sunday, January 3, 2010, 1:22:50 PM, you wrote:
>
>> 1. Your defined constants should be all CAPITALIZED.
>> 2. I ran some tests earlier last year (2009) for this and found the following:
>>     - using class constants (and accessing it via
>> ClassName::ConstantName) was about 30x faster than using define().
>>     - there were about 1000 constants used (it's a pretty big and
>> configurable app).
>>     - the tests were run using ab (with the top 10% and bottom 10%
>> edge results thrown out)
>>     - the tests were run with apc and without apc.
>
>> Will it make or break your app when it's under stress?  Definitely not.
>
>> Konstantin
>
>
>
>
>> On Sun, Jan 3, 2010 at 12:02 AM,  <webmaster at vbplusme.com> wrote:
>>> Hello ,
>>>
>>>        Happy New Year to all,
>>>
>>>
>>>  I have two statements to make a database connection. One of them
>>>  using define statement variables to parse the information to the
>>>  function to connect to the database, i.e.
>>>
>>>  open_connect(db_user, db_pass, db_name, db_host = 'localhost', $show_error=1)
>>>
>>>  The second example uses assigned variables, for example,
>>>
>>>  open_connect($db_user, $db_pass, $db_name, $db_host = 'localhost', $show_error=1)
>>>
>>>  Seems to me that both of these techniques do the same thing so I was
>>>  wondering why one would be any advantage over the other, though I
>>>  just read that the define variables take twice as long to process
>>>  under some conditions. Can some one explain which technique is the
>>>  preferred method or whether or not any compelling reason exist for
>>>  using one over the other?
>>>
>>>  Thanks in advanced for any comments.
>>>
>>>
>>> --
>>> Best regards,
>>>  Webmaster                          mailto:webmaster at vbplusme.com
>>>
>>> _______________________________________________
>>> New York PHP Users Group Community Talk Mailing List
>>> http://lists.nyphp.org/mailman/listinfo/talk
>>>
>>> http://www.nyphp.org/Show-Participation
>>>
>> _______________________________________________
>> New York PHP Users Group Community Talk Mailing List
>> http://lists.nyphp.org/mailman/listinfo/talk
>
>> http://www.nyphp.org/Show-Participation
>
>> __________ Information from ESET NOD32 Antivirus, version of virus
>> signature database 4738 (20100102) __________
>
>> The message was checked by ESET NOD32 Antivirus.
>
>> http://www.eset.com
>
>
>
>
> Thanks for the update. My example here is flawed, the constants in my
> sources are indeed all in upper case.
>
> Are you suggesting that the username, password and datbase name all be
> class constants and hard coded in the class definition? Sorry if this
> is a dumb question, I am trying to get my head around PHP OOP and am
> struggling a bit with the concepts. I am working with a database class
> that I got from phpclasses.org to try to understand how things work.
>
> --
> Best regards,
>  Webmaster                            mailto:webmaster at vbplusme.com
>
> _______________________________________________
> New York PHP Users Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/Show-Participation
>



More information about the talk mailing list