467,104 Members | 1,013 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,104 developers. It's quick & easy.

PHP, OOP, and Constants

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
Dec 18 '07 #1
  • viewed: 1527
Share:
3 Replies
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.
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.

Regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Dec 18 '07 #2
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.
Dec 18 '07 #3
boris <bk******@gmail.comwrote:
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
<?php

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
\/_\/ www.icosaedro.it

Dec 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Amadelle | last post: by
8 posts views Thread by Marty | last post: by
34 posts views Thread by newsposter0123 | last post: by
17 posts views Thread by Neil Cerutti | last post: by
2 posts views Thread by Leslie Sanford | last post: by
54 posts views Thread by shuisheng | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.