PHP, OOP, and Constants

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi. If somebody can help out with this design question, thank you very
much. Suppose I have a global constant, like an images directory. This
constant will be referenced from multiple classes. How do I define
this constant only once while using OOP? I could define the constant
on one of the classes and then reference it with ClassName::const from
the other classes, but this ties the constants to one class
arbitrarily. In Java you can define constants in an interface, and any
class implementing the interface automatically inherits the constants.
Is this possible in PHP5? I looked around the Web, but couldn't find
an example to that effect. Thank you!

~ Boris

Re: PHP, OOP, and Constants

Quoted text here. Click to load it

So let's face it, your classes will be tied to the environment of the
constants anyway, just as in java. If you have any "central" ojbect that
is visible throughout your code (an Application object, for instance),
you could consider putting them there.

It is not nice OO-programming, but personally I have some classes that
"magically" incorporate some settings (from the $GLOBALS array), such as
a ConfiguredMailer which is just a PHPMailer that sets its own from
address, encoding, mailer type, etc.

In other languages, I used to have a Settings object that was a wrapper
around INI files, registry settings, command-line parameters, etc.

Willem Bogaerts

Application smith
Kratz B.V.

Re: PHP, OOP, and Constants

Willem - thanks for the suggestions. I guess there's no easy way to do
it. I'm using CakePHP, however, and it seems adding
define('constname','constvalue') statements to /cake/app/config/
core.php makes the constants universally available. These constant
definitions belong, ideally, in a separate configuration file, but,
for now, this is an 'okay' solution.

Re: PHP, OOP, and Constants

Quoted text here. Click to load it

i have a 'site' static class wherein i define paths for the rest of the  
site. site.cfg.php is the name of the script that sets all of the paths up.  
it would look something like:

site::initialize(); // instanciate global static class
site::$myPath = '/cake/app/config/';

the site class would look something like:

class site
  public $myPath = '/';
  private __contruct();
  public initialize();

at the beginning of each page on my site, i have this:

require_once 'site.cfg.php';

i don't think that defining a path in a class is a wise thing to  
do...especially when there are multiple classes that my need to be changed  
in order to get your site up and running...such as a db class (changing the  
connection details, etc.). that's why i do it all in one script,  

as for constants in can do that in php 5. though it's not a  
real namespace, you get similar behavior. just define your variable/constant  
in the class as:

const myPath = '/cake/app/config/';

i do this with image types:

class imageTypes
   const GD  = 0;
   const GIF = 1;
   const JPG = 2;
   const PNG = 3;

this is far from arbitrary...and is accessed as you say, imageTypes::PNG.  
though i haven't tried - in this example, i don't need to - to  
inherit/extend a class with constants, i'm almost positive it would retain  
them, just as java and other oop-enabled languages do. i'd be interested in  
what you find out...i usually rtfm when i need to, and i just haven't run  
across the need to anwer this one definitively yet.


Re: PHP, OOP, and Constants

Quoted text here. Click to load it


interface MyConstants {
    const IMAGES_DIR = "some/path/";

class MyClass implements MyConstants {

    function test()
        echo "Images are here: ", self::IMAGES_DIR, "\n";


$o = new MyClass();
$o->test();  // ==>  Images are here: some/path/


Best regards,
/_|_\  Umberto Salsi

Site Timeline