Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

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

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
trickyidiot
Asked:
trickyidiot
  • 2
2 Solutions
 
Jaime OlivaresCommented:
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
 
hernst42Commented:
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
 
Jaime OlivaresCommented:
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
 
trickyidiotAuthor Commented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now