You are here: Re: [PHP] Class Constant PHP 5 « PHP « IT news, forums, messages
Re: [PHP] Class Constant PHP 5

Posted by Jochem Maas on 12/08/05 11:59

Jay,

gonna have to correct you on this lot.... (sorry ;-)

Jay Blanchard wrote:
> [snip]
> is there a way to dynamically define a class constant during runtime
> in PHP 5?
>
> for example I would like to achieve the result of something like:
>
> class Example {
> const FOO = bar();
> }
>
> However this would obviously give a parse error.
>
> I know it is possible with variables but I would like it to be a
> constant.
> [/snip]
>
> Well, first of all the syntax you describe above does not define a constant
> at all, you would need to use define()

the syntax is fine (apart from the function call which is illegal where it is),
it defines a class constant. e.g.

<?php

class Test
{
const MY_CLASS_CNST = 'qux';
}
echo Test::MY_CLASS_CNST;

?>

which is legal.

>
> The second thing is good old basic OOP theory, you should declare a private
> static variable

.... with a public static getter method (so that value can be reached from outside
the class, just like a class constant can)

>
> http://us3.php.net/private
> http://us3.php.net/manual/en/language.oop5.static.php

I agree that this is the sane/correct way to do what the OP wants.
there is one alternative (but it comes with big neon warning signs):

http://php.net/manual/en/function.runkit-constant-redefine.php

runkit is very very clever - lots of rope to hang yourself - use at
your own risk :-)

>
> Of course you could define a global constant and then pass it into your
> object when instantiating it, but that is a bad idea generally.
>
> Thirdly, you could never use a function to derive your constant value...it
> would then be an oxymoron. If the value generated by the function bar()
> changes, FOO is a variable. Constants are for simple values. For instance,
> we can all agree that pi is 3.14159 (to 5 decimal places, so defining a
> constant pi makes sense;
>
> define("PI", 3.14159);
>
> If we do not know what the outcome of a function will be it makes the value
> of the outcome a variable, always. It would be foolish (and would fail
> anyhow) to do something like this;
>
> define("RANDOM", rand(5,12));

it doesn't fail and is not imho foolish by definition ... the value of the constant,
although changing stays the same for the duration of the request,

IIRC Rasmus himself once mentioned that it can be useful to be able to set
a constant to a 'dynamic' value like this - nuff said really :-)

>

 

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

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