Jump to content

[SOLVED] case-insensitive Class Constants possible?


alexweber15

Recommended Posts

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
Share on other sites

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
Share on other sites

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  ;D

 

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
Share on other sites

:) 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
Share on other sites

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  :P;D

Link to comment
Share on other sites

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
Share on other sites

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 :D

Link to comment
Share on other sites

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 :D

 

Heh, always learning something new (not that I'll never use this bit of trivia).

Link to comment
Share on other sites

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!  8)

 

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
Share on other sites

Code as if the flag doesn't exist :D 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 :P

Link to comment
Share on other sites

Code as if the flag doesn't exist :D 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 :P

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.