Re: [PHP] Class Constants
On 13 April 2011 19:04, Floyd Resler wrote: > That didn't quite work. Here's what I did: > $const=$argv[1]; > $value=email::$const; > > When I run it I get an "Access to undeclared static property" error. > > Thanks! > Floyd > > On Apr 13, 2011, at 1:16 PM, Richard Quadling wrote: > >> On 13 April 2011 17:45, Floyd Resler wrote: >>> I'm doing some testing from the command line and would like to be able = >>> to pass along a constant from a class. For example: >>> php emailTest.,php OrderConfirmation >>> >>> OrderConfirmation is a constant in a class. Is there any way I can take = >>> the value passed on the command line and have PHP figure out which = >>> constant is equals? >>> >>> Thanks! >>> Floyd >> >> In your script ... >> >> > print_r($argv); // Requires ini setting to set argv and argc. >> // or >> print_r($GLOBALS['argv']); // >> ?> >> >> Once you've got the value, you can ... >> >> $s_ConstantValue = className::$s_ConstantNameFromCommandLine; >> >> sort of thing. >> >> >> -- >> Richard Quadling >> Twitter : EE : Zend >> @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Or http://uk.php.net/manual/en/function.constant.php constant('bar::'. $const) -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Constants
David, That worked great! Thanks! Floyd On Apr 13, 2011, at 2:25 PM, David Harkness wrote: > On Wed, Apr 13, 2011 at 11:04 AM, Floyd Resler wrote: > >> That didn't quite work. Here's what I did: >> $const=$argv[1]; >> $value=email::$const; >> > > Instead try this: > > $const = $argv[1]; > $reflector = new ReflectionClass('email'); > $value = $reflector->getConstant($const); > > David -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Constants
On Wed, Apr 13, 2011 at 11:04 AM, Floyd Resler wrote: > That didn't quite work. Here's what I did: > $const=$argv[1]; > $value=email::$const; > Instead try this: $const = $argv[1]; $reflector = new ReflectionClass('email'); $value = $reflector->getConstant($const); David
Re: [PHP] Class Constants
That didn't quite work. Here's what I did: $const=$argv[1]; $value=email::$const; When I run it I get an "Access to undeclared static property" error. Thanks! Floyd On Apr 13, 2011, at 1:16 PM, Richard Quadling wrote: > On 13 April 2011 17:45, Floyd Resler wrote: >> I'm doing some testing from the command line and would like to be able = >> to pass along a constant from a class. For example: >> php emailTest.,php OrderConfirmation >> >> OrderConfirmation is a constant in a class. Is there any way I can take = >> the value passed on the command line and have PHP figure out which = >> constant is equals? >> >> Thanks! >> Floyd > > In your script ... > > print_r($argv); // Requires ini setting to set argv and argc. > // or > print_r($GLOBALS['argv']); // > ?> > > Once you've got the value, you can ... > > $s_ConstantValue = className::$s_ConstantNameFromCommandLine; > > sort of thing. > > > -- > Richard Quadling > Twitter : EE : Zend > @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Constants
On 13 April 2011 17:45, Floyd Resler wrote: > I'm doing some testing from the command line and would like to be able = > to pass along a constant from a class. For example: > php emailTest.,php OrderConfirmation > > OrderConfirmation is a constant in a class. Is there any way I can take = > the value passed on the command line and have PHP figure out which = > constant is equals? > > Thanks! > Floyd In your script ... Once you've got the value, you can ... $s_ConstantValue = className::$s_ConstantNameFromCommandLine; sort of thing. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class constants
Gary . wrote: > On Mon, Apr 19, 2010 at 3:12 PM, Ashley Sheridan wrote: >> On 19 April 2010 14:24, Gary wrote: > >>> Okay. Why not? > ... >> Class constants must be defined with static values, not variables. They are >> constants after all! If they relied on the value of a variable, surely that >> would mean that their own value might change, so they would just become >> regular variables not constants. a constant is something constant (doesn't change), something static is something static (persistently there, but may change) > Right. But in fact the only referenced values are *also* constant > (unless I'm completely misunderstanding the use of 'const'), so I > think it's a valid thing to want to do. I accept it doesn't seem to be > possible, I'm now curious as to the thinking behind it. > >> Is there a specific reason that you need to try and achieve this? > > Okay, well here's a more meaningful situation, perhaps: > class SomeTable > { > const TABLE_NAME = 'mytable'; > const SELECT = 'SELECT * FROM ' . MyTable::TABLE_NAME; > const INSERT = 'INSERT INTO ' . MyTable::TABLE_NAME ... > try public static $INSERT = 'INSERT INTO ' . MyTable::$TABLE_NAME ... self::$INSERT > If the table name should change for some reason, it is preferable to > make the change in one place in the code (i.e. the value of > TABLE_NAME), surely, than changing the name in the alternative, which > is something like: > class SomeTable > { > // const TABLE_NAME = 'mytable'; > const SELECT = 'SELECT * FROM mytable'; > const INSERT = 'INSERT INTO mytable... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class constants
On Mon, Apr 19, 2010 at 7:25 AM, Peter Lind wrote: > Per the PHP manual: "The value must be a constant expression". Is > something that depends on other classes, variables or functions > constant? > When I came up against this problem myself, I read "a constant expression" to mean "an expression involving only constants". I think that's a fairly reasonable parse of the phrase. Thus I expected to be able to combine other constants using the dot operator as Gary is trying to do. Obviously, it didn't work, and I removed the "base" constant and replicated its value in the other constants--a situation constants were designed to obviate. Gary, you'd probably be better off asking for the technical reason behind this limitation on a -devel list. :) -- David Harkness Senior Software Engineer High Gear Media, Inc.
Re: [PHP] Class constants
On Mon, Apr 19, 2010 at 10:25 AM, Peter Lind wrote: > On 19 April 2010 16:18, Gary . wrote: > > On Mon, Apr 19, 2010 at 2:37 PM, Peter Lind > wrote: > >> On 19 April 2010 14:24, Gary wrote: > >>> On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote: > > > So no, you shouldn't be able to do that. > >>> > >>> Okay. Why not? > >> > >> Hate to ask, but did you at any point consider to read the PHP docs on > >> this? The bit I sent or what you could gather from the link posted? > > > > Yes. The question remains. > > > > Per the PHP manual: "The value must be a constant expression". Is > something that depends on other classes, variables or functions > constant? > If you're asking why a constant should be constant, I can only point > you to Ashleys answer or google. > > Regards > Peter > > -- > > WWW: http://plphp.dk / http://plind.dk > LinkedIn: http://www.linkedin.com/in/plind > Flickr: http://www.flickr.com/photos/fake51 > BeWelcome: Fake51 > Couchsurfing: Fake51 > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Hi Gary, Sorry for the confusion. I understand your question. For the life of those variables, they would never change If you come from other programming language backgrounds, this can be a bit confusing. In Objective C, the term "constant expression" is one in which every element is a constant, so given the requirement that "The value must be a constant expression", your examples would make sense. Additionally, in languages like Scala, values default to a constant, and you can compose them in a myriad of ways. I don't claim to know the rationale, but once you get used to it, it's not a big deal. I just tend to use classes that allow a value to be set once and only once for these types of situations. Happy coding ;) Adam -- Nephtali: PHP web framework that functions beautifully http://nephtaliproject.com
Re: [PHP] Class constants
On 19 April 2010 16:18, Gary . wrote: > On Mon, Apr 19, 2010 at 2:37 PM, Peter Lind wrote: >> On 19 April 2010 14:24, Gary wrote: >>> On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote: > So no, you shouldn't be able to do that. >>> >>> Okay. Why not? >> >> Hate to ask, but did you at any point consider to read the PHP docs on >> this? The bit I sent or what you could gather from the link posted? > > Yes. The question remains. > Per the PHP manual: "The value must be a constant expression". Is something that depends on other classes, variables or functions constant? If you're asking why a constant should be constant, I can only point you to Ashleys answer or google. Regards Peter -- WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class constants
On Mon, Apr 19, 2010 at 3:12 PM, Ashley Sheridan wrote: > On 19 April 2010 14:24, Gary wrote: > > Okay. Why not? ... > Class constants must be defined with static values, not variables. They are > constants after all! If they relied on the value of a variable, surely that > would mean that their own value might change, so they would just become > regular variables not constants. Right. But in fact the only referenced values are *also* constant (unless I'm completely misunderstanding the use of 'const'), so I think it's a valid thing to want to do. I accept it doesn't seem to be possible, I'm now curious as to the thinking behind it. > Is there a specific reason that you need to try and achieve this? Okay, well here's a more meaningful situation, perhaps: class SomeTable { const TABLE_NAME = 'mytable'; const SELECT = 'SELECT * FROM ' . MyTable::TABLE_NAME; const INSERT = 'INSERT INTO ' . MyTable::TABLE_NAME ... If the table name should change for some reason, it is preferable to make the change in one place in the code (i.e. the value of TABLE_NAME), surely, than changing the name in the alternative, which is something like: class SomeTable { // const TABLE_NAME = 'mytable'; const SELECT = 'SELECT * FROM mytable'; const INSERT = 'INSERT INTO mytable... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class constants
On Mon, Apr 19, 2010 at 2:37 PM, Peter Lind wrote: > On 19 April 2010 14:24, Gary wrote: >> On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote: >>> So no, you shouldn't be able to do that. >> >> Okay. Why not? > > Hate to ask, but did you at any point consider to read the PHP docs on > this? The bit I sent or what you could gather from the link posted? Yes. The question remains. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class constants
On Mon, 2010-04-19 at 14:37 +0200, Peter Lind wrote: > On 19 April 2010 14:24, Gary . wrote: > > On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote: > >> On 19 April 2010 10:30, Gary wrote: > >>> Should I be able to do this: > >>> > >>> class X > >>> { > >>> const FOO = 'foo'; > >>> const FOOBAR = X::FOO . 'bar'; > >>> > >>> ... > >>> } > > > >> So no, you shouldn't be able to do that. > > > > Okay. Why not? > > Hate to ask, but did you at any point consider to read the PHP docs on > this? The bit I sent or what you could gather from the link posted? > > -- > > WWW: http://plphp.dk / http://plind.dk > LinkedIn: http://www.linkedin.com/in/plind > Flickr: http://www.flickr.com/photos/fake51 > BeWelcome: Fake51 > Couchsurfing: Fake51 > > Class constants must be defined with static values, not variables. They are constants after all! If they relied on the value of a variable, surely that would mean that their own value might change, so they would just become regular variables not constants. Is there a specific reason that you need to try and achieve this? Would it not be better to have private or protected variables using getters and setters if you need variable values? Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Class constants
On 19 April 2010 14:24, Gary . wrote: > On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote: >> On 19 April 2010 10:30, Gary wrote: >>> Should I be able to do this: >>> >>> class X >>> { >>> const FOO = 'foo'; >>> const FOOBAR = X::FOO . 'bar'; >>> >>> ... >>> } > >> So no, you shouldn't be able to do that. > > Okay. Why not? Hate to ask, but did you at any point consider to read the PHP docs on this? The bit I sent or what you could gather from the link posted? -- WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class constants
On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote: > On 19 April 2010 10:30, Gary wrote: >> Should I be able to do this: >> >> class X >> { >> const FOO = 'foo'; >> const FOOBAR = X::FOO . 'bar'; >> >> ... >> } > So no, you shouldn't be able to do that. Okay. Why not? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class constants
On 19 April 2010 10:30, Gary . wrote: > Should I be able to do this: > > class X > { > const FOO = 'foo'; > const FOOBAR = X::FOO . 'bar'; > > ... > } > > ? > > Because I can't. I get "syntax error, unexpected '.', expecting ',' or > ';'". I assume this is because the constants are like statics which > can't be initialised by functions etc. but is there really any logic > behind this? > It very often pays to read the PHP docs. From http://pl.php.net/manual/en/language.oop5.constants.php : "The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call. " So no, you shouldn't be able to do that. -- WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] class constants
getConstants()); ?> Thank you, I've try this above and works, I hadn't seen the message below. Excuse my english. On 3/7/06, Jared Williams < [EMAIL PROTECTED]> wrote: > > > > > > Hi, > > > > ReflectionClass > > > > getConstant > > getConstants > > hasConstant > > > > http://www.ren.dotgeek.org/classbrowser/class.php?class=ReflectionClass > > > > Jared > > >
Re: [PHP] class constants
I've try this with no successful results. the constant rr and gg were not in the array $ee. $value) echo $key." => ".$value.""; ?> On 3/7/06, Jared Williams <[EMAIL PROTECTED]> wrote: > > > Hi, > > ReflectionClass > > getConstant > getConstants > hasConstant > > http://www.ren.dotgeek.org/classbrowser/class.php?class=ReflectionClass > > Jared > > > -Original Message- > > From: Arnaldo Gandol [mailto:[EMAIL PROTECTED] > > Sent: 07 March 2006 21:03 > > To: php-general@lists.php.net > > Subject: [PHP] class constants > > > > how can I to iterate the class constant without knowing their > > names?, I've reviewed the Class/Object Functions and > > Reflexion and nothing, I dont want to use static class > > variables, can any one help. > > > >
RE: [PHP] class constants
Hi, ReflectionClass getConstant getConstants hasConstant http://www.ren.dotgeek.org/classbrowser/class.php?class=ReflectionClass Jared > -Original Message- > From: Arnaldo Gandol [mailto:[EMAIL PROTECTED] > Sent: 07 March 2006 21:03 > To: php-general@lists.php.net > Subject: [PHP] class constants > > how can I to iterate the class constant without knowing their > names?, I've reviewed the Class/Object Functions and > Reflexion and nothing, I dont want to use static class > variables, can any one help. > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] class constants
Shaunak Kashyap wrote: Constants aren't per class, they are per system. Whether you define them in a class or not won't change this. You can list all constants using http://www.php.net/manual/en/function.get-defined-constants.php Actually, PHP5 allows constants per class, aka class constants. I think that's what the OP is referring to. I have been educated :) I didn't know they existed. They don't show up in var_dump or anything like it :/ From the looks of things you can't get them - maybe put in a feature request (bugs.php.net) and see what happens for the next version. -- Postgresql & php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] class constants
> > Constants aren't per class, they are per system. Whether you define them > in a class or not won't change this. > > You can list all constants using > http://www.php.net/manual/en/function.get-defined-constants.php > > Actually, PHP5 allows constants per class, aka class constants. I think that's what the OP is referring to. Shaunak Kashyap Senior Web Developer WPT Enterprises, Inc. 5700 Wilshire Blvd., Suite 350 Los Angeles, CA 90036 Direct: 323.330.9870 Main: 323.330.9900 www.worldpokertour.com Confidentiality Notice: This e-mail transmission (and/or the attachments accompanying) it may contain confidential information belonging to the sender which is protected. The information is intended only for the use of the intended recipient. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or taking of any action in reliance on the contents of this information is prohibited. If you have received this transmission in error, please notify the sender by reply e-mail and destroy all copies of this transmission. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] class constants
Arnaldo Gandol wrote: how can I to iterate the class constant without knowing their names?, I've reviewed the Class/Object Functions and Reflexion and nothing, I dont want to use static class variables, can any one help. Constants aren't per class, they are per system. Whether you define them in a class or not won't change this. You can list all constants using http://www.php.net/manual/en/function.get-defined-constants.php If you want to look at the class variables: http://www.php.net/manual/en/function.get-class-vars.php -- Postgresql & php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class constants
Marcus Bointon wrote: I'm not sure if this is a bug or a feature, but it seems you can't use class constants to set default values for class properties. You can, however, use them for default values for method params, e.g.: class foo {} ^--- er. const BAR = 100; private $thing = self::BAR; function wibble($a = self::BAR) { echo $a; } } class foo { const BAR = 100; private $thing = self::BAR; function wibble($a = self::BAR) { echo $a; } } $f = new foo; $f->wibble(); it _seems_ you did not test this, at least when I run this it gives me a fatal error (which definitely leaves $this->thing undefined!) In this case $this->thing will be undefined, but wibble() will get the correct default value for $a. Comments? I believe this is due to the fact that when setting up $thing, php is still building the class definition therefore the constant does not 'exist' yet. the error msg I get seems to confirm this. this does work: class qux { const BAR = 100; } class foo { const BAR = 100; private $thing = qux::BAR; function wibble($a = self::BAR) { echo $a,"\n",$this->thing; } } $f = new foo; $f->wibble(); this does work, which really surprised me. class foo { const BAR = 100; private $thing = foo::BAR; function wibble($a = self::BAR) { echo $a,"\n",$this->thing; } } $f = new foo; $f->wibble(); but honestly if you want to be very stricly correct about you OO then you should not (I believe) be setting a value to an instance member in the class definition, instead do: class foo { const BAR = 100; function __construct() { $this->thing = self::BAR; } function wibble($a = self::BAR) { echo $a,"\n",$this->thing; } } $f = new foo; $f->wibble(); I tested all this on PHP 5.0.2 (cli) (built: Oct 21 2004 13:52:27) Marcus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php