Dan
Dan

Reputation: 4140

PHP - Access object properties without case sensitivity?

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.

Upvotes: 8

Views: 5074

Answers (4)

Paul Zepernick
Paul Zepernick

Reputation: 1462

I know this is a old post, but I thought I would throw another solution out there. I initially tried the get_class_vars approach, but decided to cast to a array instead and then use array_change_key_case to convert the keys to lower case. At this point you could either work with the array or convert back to an object. I have not done any benchmark's, but I am dealing with small objects.

function convertPropNamesLower($obj) {
    return (object)array_change_key_case((array)$obj, CASE_LOWER);
}

//usage
$myObj = convertPropNamesLower($myObj);
echo $myobj -> myprop;

Upvotes: 2

martinstoeckli
martinstoeckli

Reputation: 24081

If you have no control over the object (maybe it's a row from an external database), i would recommend to find out the correct names before you access the rows. The function below is not a cheap operation, but done only the first time, it can make your application more stable.

function findPropertyNameCaseInsensitive($obj, $name)
{
  $propertyNames = array_keys(get_object_vars($obj));
  foreach($propertyNames as $propertyName)
  {
    if (strcasecmp($name, $propertyName) == 0)
      return $propertyName;
  }
  return NULL;
}

Upvotes: 4

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124768

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.

Upvotes: 5

svens
svens

Reputation: 11628

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).

Upvotes: 0

Related Questions