Solved

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

Posted on 2008-10-03
4
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This question got me thinking... (http://www.experts-exchange.com/questions/28707487/GLOBALS.html) Why shouldn't we use Globals? This is a simple question without a simple answer.  How do you explain these concepts to a programmer w…
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

622 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