Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-03
4
Medium Priority
?
370 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 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
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 dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

704 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