Solved

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

Posted on 2008-10-03
4
348 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Before You Read The Article Please make sure you understand these two concepts: Variable Scope (http://www.php.net/manual/en/language.variables.scope.php) and Property Visibility (http://www.php.net/manual/en/language.oop5.visibility.php).  And to …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

762 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

12 Experts available now in Live!

Get 1:1 Help Now