Solved

PHP: defined constants versus class-defined variables - need some valid opinions

Posted on 2008-10-03
4
349 Views
Last Modified: 2013-11-12
I'm in the beginning stages of building a very large application and I'm at a cross-roads.

Do I create an include file that uses define() to define a plethora of constants for all vars used application-wide allowing me to reference them like this:
echo "This is something and " . SOME_VALUE . "<br />";

OR

Do I create a class, instantiated by the core class that houses all of the variables, allowing me to reference them like this:
echo "This is something and {$this->main->coreVariables->someValue}<br />";

I want opinions from the experts on performance, tidy-ness, best practice, etc.

I will award points evenly between everyone, excluding those who merely post nonsense to get free points.
0
Comment
Question by:trickyidiot
  • 2
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 22638447
Creating a class, just to store those variables doesn't sound logic to me.
The usual OO practice is to put the constant variables inside the clases that belong to, not all in the same class.
As an alternative you can put all those variables in a single collection, just like the $SESSION array, but it is the same unelegant.
0
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 250 total points
ID: 22638655
defines are slower than class constants. http://www.ipersec.com/index.php/2007/10/16/define-or-const-members/

The advantage of class constants is that you can use autoloading of classes for that. So if the constant is used the class will be included.

Also using class constants (Myclass::SOMEVALUE) is good OOP style
The main performance problems when building big php application is the bootstrapping of you framework to include all necessary classes. If you have a lot of them it will cost alot of time to parse and compile them for each request. So keep in mind to design your application to use auotloading of your classes
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22638689
about this comment and continuing taking about good OOP style:
>> The main performance problems when building big php application is the bootstrapping
>> of you framework to include all necessary classes
If you need a constant than belongs a class, then you need the class, unless you have placed the constant in the wrong class.
0
 
LVL 6

Author Closing Comment

by:trickyidiot
ID: 31502934
Thinking more into it, Jaime is right in that having all my vars in one place, rather than in the classes they are referenced is a bad idea.
I will have my database vars defines inside my database class, my navigation vars inside my output formatting class, etc.
So far as managing the application goes, it makes more sense.
Were I building this application to be duplicated for multiple companies, I would have gone more with a configuration file approach, but since this is for one company, I will go ahead with the separation by class.

Thank all of you for your input. I am splitting the points evenly as promised.
-=Patrick=-
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now