views:

118

answers:

3

Hi,

I'm working on an old app where there is a lot of inconsistency in the naming conventions used.

Is there a way to access object properties that ignores case sensitivity?

For example, I have an object called currentuser with the attribute Name.

Is there any way to retrieve that value like this?

$currentuser->name

Any advice appreciated.

Thanks.

+3  A: 

Well, you can write a __get function to each of your classes that would handle such a conversion, but it's quite hacky. Something like this might work:

class HasInconsistentNaming {

    var $fooBar = 1;
    var $Somethingelse = 2;

    function __get($var) {
        $vars = get_class_vars(get_class($this));
        foreach($vars as $key => $value) {
            if(strtolower($var) == strtolower($key)) {
                return $this->$key;
                break;
            }
        }
        return null;
    }
}

Now, you can do this:

$newclass = new HasInconsistentNaming();

echo $newclass->foobar; // outputs 1

If you want to simplify your task a bit you can have your base classes inheriting from a class that provides this functionality. This way you don't have to write the function to each of your classes:

class CaseInsensitiveGetter {
    function __get($var) {
        $vars = get_class_vars(get_class($this));
        foreach($vars as $key => $value) {
            if(strtolower($var) == strtolower($key)) {
                return $this->$key;
                break;
            }
        }
        return null;
    }
}

class HasInconsistentNaming extends CaseInsensitiveGetter {
    var $fooBar = 1;
    var $Somethingelse = 2;
}

But I'd strongly discourage you from taking this approach. In the long run, it would be much smarter to just convert all variables into a consistent naming scheme.

Tatu Ulmanen
+1 as I wanted to suggest the same. You beat me to it. I'd have it throw an Exception though if the property is not found at all.
Gordon
Thanks, I'd prefer not to have to do it, but I don't have much choice in the short-term at least.
Dan
A: 

Interesting question. There are two possible ways to achieve what you want. Either you can use foreach to interate through the array or have a look at the php reflection functionalities (http://www.php.net/manual/en/book.reflection.php).

svens
A: 

Not directly but you could create some kind of wrapper function:

function get_property($object, $property) {
    // check for lower case property
    $property = strtolower($property);
    if(property_exists($object, $property)) {
         return $object->$property;
    }
    // check for capitalized property
    $property = ucwords($property);
    if(property_exists($object, $property)) {
         return $object->$property;
    }
    else {
       throw new Exception("Property does not exist.");
    }
}

and use it like this:

$name = get_property($currentuser, 'name');

This way you don't have to touch any class. It just might not be that readable.

See property_exists, ucwords.

Felix Kling