tags:

views:

2287

answers:

8

How do I get a property in a PHP based on a string? I'll call it magic. So what is magic?

$obj->Name = 'something';
$get = $obj->Name;

would be like...

magic($obj, 'Name', 'something');
$get = magic($obj, 'Name');
+6  A: 

Like this

<?php

$prop = 'Name';

echo $obj->$prop;

Or, if you have control over the class, implement the ArrayAccess interface and just do this

echo $obj['Name'];
Peter Bailey
Thanks a bunch!
Daniel A. White
+1  A: 

Something like this? Haven't tested it but should work fine.

function magic($obj, $var, $value = NULL)
{
    if($value == NULL)
    {
        return $obj->$var;
    }
    else
    {
        $obj->$var = $value;
    }
}
Ólafur Waage
Just tested it, works fine.
Ólafur Waage
+1  A: 

Just store the property name in a variable, and use the variable to access the property. Like this:

$name = 'Name';

$obj->$name = 'something';
$get = $obj->$name;
Jon Benedicto
+1  A: 

What you're asking about is called Variable Variables. All you need to do is store your string in a variable and access it like so:

$Class = 'MyCustomClass';
$Property = 'Name';
$List = array('Name');

$Object = new $Class();

// All of these will echo the same property
echo $Object->$Property;  // Evaluates to $Object->Name
echo $Object->{$List[0]}; // Use if your variable is in an array
sirlancelot
Variable variables are another thing.
Ólafur Waage
The question is how to get a class property (variable) when the name is contained in a string (variable). Or did I misread the question?
sirlancelot
A: 

Try

function magic(){
   if(func_num_args() == 2 || func_num_args() == 3){
     $obj = func_get_arg(0);
     $key = func_get_arg(1);
     if(func_num_args() == 3)
       return $obj->$key = func_get_arg(2);
     else
       return $obj->$key;
   }else
      throw new Exception('Function requires 2 or 3 arguments!');
}
scragar
A: 
VolkerK
A: 

user eval instead of doing all of that.

$object = new className();

$string="magic";
eval("$yourVar = $object->".$string);
echo $yourVar;
r4ccoon
A: 

Here is my attempt. It has some common 'stupidity' checks built in, making sure you don't try to set or get a member which isn't available.

You could move those 'property_exists' checks to __set and __get respectively and call them directly within magic().

<?php

class Foo {
    public $Name;

    public function magic($member, $value = NULL) {
        if ($value != NULL) {
            if (!property_exists($this, $member)) {
                trigger_error('Undefined property via magic(): ' .
                    $member, E_USER_ERROR);
                return NULL;
            }
            $this->$member = $value;
        } else {
            if (!property_exists($this, $member)) {
                trigger_error('Undefined property via magic(): ' .
                    $member, E_USER_ERROR);
                return NULL;
            }
            return $this->$member;
        }
    }
};

$f = new Foo();

$f->magic("Name", "Something");
echo $f->magic("Name") , "\n";

// error
$f->magic("Fame", "Something");
echo $f->magic("Fame") , "\n";

?>
Nick Presta