You are here: Re: PHP, OOP, and Constants « PHP Programming Language « IT news, forums, messages
Re: PHP, OOP, and Constants

Posted by Umberto Salsi on 12/18/07 21:22

boris <bkerzner@gmail.com> wrote:

> 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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация