Re: [PHP] Class Constant PHP 5
Jay Blanchard wrote: [snip] 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 :-) [/snip] Thanks for the education there Jochem, I'd forgotten about const mostly because I am in the habit of declaring private static variables and using get and set methods for this kind of thing because I do a lot of C++ work too and the theory holds up in both languages. And it is correct in the fact that a class constant cannot contain a function (as I hadn't clearly stated earlier) as the OP had requested, the syntax would fail. true - although the classkit extension would allow you to hack the class and set the constant to a return value of a function. doing it your way would be _much_ better though :-) I guess that I am old school enough (I see John Nichel's hands racing to the keyboard now!) that expect a constant to be just that. The concept of a changing constant is what we would normally call a variable. I suppose that but a constant doesn't change through out a single request, only between requests. as far as the script is concerned the value doesn't ever change (given the 'share nothing' principle). one reason for using constants in compiled code is so that you can change behaviour of code at compile time no? well a constant as follows in php ammounts to the same: define('SOME_ENV_THINGY', getMyEnvVal()); only compiling is done a little more often. a holy war could ensue over this. In the end a class constant is constant your in a fighting mood heh ;-) given that windows is the spawn of satan ... isn't a crusade started by a windows shop a little contradictory ;-) and its visibility is public. For the OP I found the following manual page; http://us2.php.net/manual/en/language.oop5.constants.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Constant PHP 5
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 :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Class Constant PHP 5
[snip] 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 :-) [/snip] Thanks for the education there Jochem, I'd forgotten about const mostly because I am in the habit of declaring private static variables and using get and set methods for this kind of thing because I do a lot of C++ work too and the theory holds up in both languages. And it is correct in the fact that a class constant cannot contain a function (as I hadn't clearly stated earlier) as the OP had requested, the syntax would fail. I guess that I am old school enough (I see John Nichel's hands racing to the keyboard now!) that expect a constant to be just that. The concept of a changing constant is what we would normally call a variable. I suppose that a holy war could ensue over this. In the end a class constant is constant and its visibility is public. For the OP I found the following manual page; http://us2.php.net/manual/en/language.oop5.constants.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Constant PHP 5
Dynamically setting a constant would break the very rule of it being a constant in the first place. a constant is something that does not change it cannot be dynamic. On Wednesday 07 December 2005 12:00, Jeffrey Sambells wrote: 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. Thanks. - Jeff ~~ Jeffrey Sambells Director of Research and Development Zend Certified Engineer (ZCE) We-Create Inc. [EMAIL PROTECTED] email 519.745.7374 office 519.897.2552 mobile ~~ Get Mozilla Firefox at http://spreadfirefox.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Class Constant PHP 5
[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 second thing is good old basic OOP theory, you should declare a private static variable http://us3.php.net/private http://us3.php.net/manual/en/language.oop5.static.php 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)); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Constant PHP 5
It would be foolish (and would fail anyhow) to do something like this; Nope. :P ?php define(RANDOM, rand(5,12)); var_dump(RANDOM); ? int(12) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Class Constant PHP 5
[snip] It would be foolish (and would fail anyhow) to do something like this; Nope. :P ?php define(RANDOM, rand(5,12)); var_dump(RANDOM); ? int(12) [/snip] Wow, that should fail. But you did have use var_dump() to get it, which may be slightly counter-intuitive. I just did this function realRand($x){ $x = $x * rand(5,10); return $x; } define(RANDOM, realRand(1.2)); var_dump(RANDOM); and it returns floats. Well, I'll be jiggered. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Class Constant PHP 5
[snip] Wow, that should fail. But you did have use var_dump() to get it, which may be slightly counter-intuitive. I just did this function realRand($x){ $x = $x * rand(5,10); return $x; } define(RANDOM, realRand(1.2)); var_dump(RANDOM); and it returns floats. Well, I'll be jiggered. [/snip] From http://us3.php.net/manual/en/language.constants.php Only scalar data (boolean, integer, float and string) can be contained in constants. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Constant PHP 5
Stephen Leaf wrote: Dynamically setting a constant would break the very rule of it being a constant in the first place. Did you say something about my Java? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Constant PHP 5
The point was more that the constant's value is 'defined' at the beginning of the script, and is constant and non changing throughout the entire execution of the script. But I was looking for a way to give it a namespace inside a class rather than just defining in in the global scope so that I do not have to worry about conflicting names with other packages such as PEAR et al. I wanted to do something like: ? define('ClassName::ConstantName',$valueDeterminedAtStartOfScript); ? so that I I could later use the notation $value = ClassName::ConstantName or from within the class $value = self::ConstantName and ensure other developers could not change the value of the constant. To achieve the result I want I could do: ? define ('foo',$valueDeterminedAtStartOfScript); class ClassName { const ConstantName = foo; } ? But that just seems pointless and messy. I will assume that the simple answer to my original question was 'No that it is not possible'. Thanks - Jeff Jeffrey Sambells cell 519.897.2552 phone 905.878.4701 web http://www.sambells.info On 7-Dec-05, at 1:22 PM, 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 second thing is good old basic OOP theory, you should declare a private static variable http://us3.php.net/private http://us3.php.net/manual/en/language.oop5.static.php 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));
[PHP] Class in PHP
Hi all, How many extension of subclass can PHP have? Can I extend subclass to more different subclass? Thanks, Calvin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Class in PHP
On Wed, 5 Dec 2001 10:41:53 -0500 Wee Chua [EMAIL PROTECTED] wrote: Hi all, How many extension of subclass can PHP have? Can I extend subclass to more different subclass? Do you mean how deep a inheritance you can have? e.g. A \ B | \ C D | \ E F There is nothing wrong with doing Class F extends D (which then in turn inherits it's stuff from class b, etc.) -- Get your firstname@lastname email for FREE at http://Nameplanet.com/?su -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]