alexweber15 Posted October 17, 2008 Share Posted October 17, 2008 weird issue i ran into today... - its possible to define case-insensitive constants outside the scope of a class using: define('name', 'value', false); however, you cannot use define() for Class Constants. - to define a Class Constant you have to use (inside class scope): const 'name' = 'value'; So what happened to case-sensitivity here? how can i define a case-insensitive Class Constant???? thanks! Alex Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/ Share on other sites More sharing options...
rhodesa Posted October 17, 2008 Share Posted October 17, 2008 simple answer: you can't Why don't use a static function? class someClass { private static $vals = array( 'name' => 'abc', ); static function getValue ( $key ) { return self::$vals[strtolower($key)]; } } print someClass::getValue('NAME'); Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668228 Share on other sites More sharing options...
alexweber15 Posted October 17, 2008 Author Share Posted October 17, 2008 why not? seems silly to me that's why... i'd rather define an uppercase and lowercase version of the same constant =P const FOO = 'bar'; const foo = self::FOO; lol just kidding! really i'd rather just stick with all-uppercase convention instead, i just think it doesn't add up because I mean if you can declare case-insensitive constants outside a class then why the hell can't you do the same inside the class? seems like poor implementation to me... Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668251 Share on other sites More sharing options...
rhodesa Posted October 17, 2008 Share Posted October 17, 2008 i never even realized there was a case-insensitive flag in define(). i just always use all caps Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668253 Share on other sites More sharing options...
alexweber15 Posted October 17, 2008 Author Share Posted October 17, 2008 i never even realized there was a case-insensitive flag in define(). i just always use all caps Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668263 Share on other sites More sharing options...
genericnumber1 Posted October 18, 2008 Share Posted October 18, 2008 I agree with this post, remove the case insensitive param in define() Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668433 Share on other sites More sharing options...
alexweber15 Posted October 18, 2008 Author Share Posted October 18, 2008 I agree with this post, remove the case insensitive param in define() lol i hope you're being ironic they should do the exact opposite and add the case insensitive param to const Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668498 Share on other sites More sharing options...
genericnumber1 Posted October 18, 2008 Share Posted October 18, 2008 I was mainly making a joke, but I do think case insensitivity is a poor choice for a developer to choose. If I have to read code and force myself to distinguish red Red rEd reD RED, and with your suggestion class::red, class:Red, etc... I think I'd just delete the code and send an angry email to the developer. Variables/attributes are case sensitive, classes definitions are case sensitive, functions/methods are case sensitive.. if you ask me, the inconsistency is that global scope constants can be case insensitive, not that object constants can't. Not that I'm suggesting they remove the case insensitive setting from define(), because I couldn't care less in my own code; I just won't use it. I'm saying that if I saw it in someone else's code I would think the developer hasn't been programming in the C/C++/PHP/Java world very long.. (I'm looking at you VB/Pascal/ASP programmers!) Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668502 Share on other sites More sharing options...
alexweber15 Posted October 18, 2008 Author Share Posted October 18, 2008 lol good point im all for case-sensitivity as it makes you write more legible and less error-prone code (somehow) but this was a particular situation where i was trying to write a friendly api for a class by including multiple aliases for each function, etc come to think of it, screw it! i'm gonna remove all aliases and people should just RTFM if they wanna use my class Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668504 Share on other sites More sharing options...
rhodesa Posted October 18, 2008 Share Posted October 18, 2008 I would recommend trying to stick to the Zend standards: http://framework.zend.com/manual/en/coding-standard.html Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668647 Share on other sites More sharing options...
corbin Posted October 18, 2008 Share Posted October 18, 2008 If the people using it aren't smart enough to not need aliases, they shouldn't be touching a PHP file, in my opinion. Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668673 Share on other sites More sharing options...
alexweber15 Posted October 18, 2008 Author Share Posted October 18, 2008 agreed on both statements! (rhodesa and corbin) i guess i just had a lapse where i got a little too concerned about who was going to use it rather than doing it properly. after all, i'm complete the documentation at the end and offer support so i should just focus on making it functional! Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668761 Share on other sites More sharing options...
corbin Posted October 18, 2008 Share Posted October 18, 2008 Eh as long as the docs are decent, and it's coded with some logic in mind, I'm sure people will be able to figure it out. Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-668783 Share on other sites More sharing options...
discomatt Posted October 20, 2008 Share Posted October 20, 2008 Just to note to genericnumber1... Functions are NOT case sensitive. <pre><?php function somefunc() { echo 'Hello World'; } someFunc(); ?></pre> I agree with the consensus here though. Variables/constants should NEVER be case-insensitive. It's sloppy, even for PHP's standards Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670415 Share on other sites More sharing options...
genericnumber1 Posted October 20, 2008 Share Posted October 20, 2008 Just to note to genericnumber1... Functions are NOT case sensitive. <pre><?php function somefunc() { echo 'Hello World'; } someFunc(); ?></pre> I agree with the consensus here though. Variables/constants should NEVER be case-insensitive. It's sloppy, even for PHP's standards Heh, always learning something new (not that I'll never use this bit of trivia). Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670421 Share on other sites More sharing options...
corbin Posted October 20, 2008 Share Posted October 20, 2008 Certain people wanted to make them case-sensitive in PHP6, but it was shot down since it would break old code. Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670434 Share on other sites More sharing options...
genericnumber1 Posted October 21, 2008 Share Posted October 21, 2008 I'm sure eventually PHP might do the bandaid rip that python is doing now. Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670529 Share on other sites More sharing options...
corbin Posted October 21, 2008 Share Posted October 21, 2008 By bandaid rip you mean change things that will break some scripts? I would be all for PHP making everything case sensitive. I'm not a pansy 3 year old programmer; my cases match. lol Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670550 Share on other sites More sharing options...
genericnumber1 Posted October 21, 2008 Share Posted October 21, 2008 By bandaid rip you mean change things that will break some scripts? Yup But yeah, I think case insensitivity is just for lazy programmers! (well, particularly lazy, we're ALL pretty lazy) Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670560 Share on other sites More sharing options...
alexweber15 Posted October 21, 2008 Author Share Posted October 21, 2008 lol i could have sworn i already said this here but its 3am and ive posted about this on google groups too but i really dont care about case-sensitivity. i think PHP is fine as it is, in fact its GREAT! i'm just a stickler for consistency. and having a case-sensitive flag (albeit optional) in "define()" and not having the same option in "const" is just wrong. constants are constants. so it makes no sense to me to allow case-insensitivity in one context and disallow it in another. personally i think that all constants should be capitalized, end of story. Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670615 Share on other sites More sharing options...
discomatt Posted October 21, 2008 Share Posted October 21, 2008 Code as if the flag doesn't exist Even with case-sensitivity, there's no way in hell you should have 2 constants with the same name in different cases. define('FOOBAR', 'someval'); define('FooBar', 'anotherval'); could lead to some major confusion. I like case insensitivity for function and class names. They're both designed as a modular way to share code.. whether it be with your own script or others. Some people prefer to code using camelHump(), or UppercaseFirst() or sometimeslower(). Having case-insensitive function/class names allows multiple developers to use the same code without having to worry about the other dev's case standards. This also goes with my above comment that you should never have two functions or classes with the same name, regardless of case differences Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670905 Share on other sites More sharing options...
alexweber15 Posted October 21, 2008 Author Share Posted October 21, 2008 Code as if the flag doesn't exist Even with case-sensitivity, there's no way in hell you should have 2 constants with the same name in different cases. define('FOOBAR', 'someval'); define('FooBar', 'anotherval'); could lead to some major confusion. I like case insensitivity for function and class names. They're both designed as a modular way to share code.. whether it be with your own script or others. Some people prefer to code using camelHump(), or UppercaseFirst() or sometimeslower(). Having case-insensitive function/class names allows multiple developers to use the same code without having to worry about the other dev's case standards. This also goes with my above comment that you should never have two functions or classes with the same name, regardless of case differences good points, like i said "personally i think that all constants should be capitalized, end of story." as far as function and variable names I think that there should be some kind of convention (made a poll here: http://www.phpfreaks.com/forums/index.php/topic,221997.0.html) Alex Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670950 Share on other sites More sharing options...
discomatt Posted October 21, 2008 Share Posted October 21, 2008 as long as someFunction(), somefunction() and SomeFunction() all work the same... is there really a need for convention? Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670954 Share on other sites More sharing options...
alexweber15 Posted October 21, 2008 Author Share Posted October 21, 2008 as long as someFunction(), somefunction() and SomeFunction() all work the same... is there really a need for convention? without convention anarchy would ensure! Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670963 Share on other sites More sharing options...
genericnumber1 Posted October 21, 2008 Share Posted October 21, 2008 as long as someFunction(), somefunction() and SomeFunction() all work the same... is there really a need for convention? I don't want to read someone's code where they aren't consistent.. it looks amateur, messy, and it makes the code harder to read. Link to comment https://forums.phpfreaks.com/topic/128887-solved-case-insensitive-class-constants-possible/#findComment-670972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.