如何根据字符串获取PHP中的属性?我将其称为 magic 。那么什么是 magic

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

会像......

magic($obj, 'Name', 'something');
$get = magic($obj, 'Name');
有帮助吗?

解决方案

喜欢这个

<?php

$prop = 'Name';

echo $obj->$prop;

或者,如果您可以控制该类,请实施 ArrayAccess 界面,只需这样做

echo $obj['Name'];

其他提示

如果要在不创建中间变量的情况下访问该属性,请使用 {} 表示法:

$something = $object->{'something'};

这也允许您在循环中构建属性名称,例如:

for ($i = 0; $i < 5; $i++) {
    $something = $object->{'something' . $i};
    // ...
}

您要问的是变量变量 。您需要做的就是将您的字符串存储在变量中并像以下一样访问它:

$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

这样的东西?没有测试过,但应该可以正常工作。

function magic($obj, $var, $value = NULL)
{
    if($value == NULL)
    {
        return $obj->$var;
    }
    else
    {
        $obj->$var = $value;
    }
}

只需将属性名称存储在变量中,然后使用该变量访问该属性。像这样:

$name = 'Name';

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

很简单,$ obj-&gt; {$ obj-&gt; Name}花括号将属性包装成变量变量。

这是一次热门搜索。但是没有解决我的问题,即使用$ this。在我的情况下使用花括号也有帮助...

使用Code Igniter获取实例

的示例 在具有父类实例

的名为something的源类库中的

$this->someClass='something';
$this->someID=34;

库类需要使用父实例

从另一个类中获取源代码
echo $this->CI->{$this->someClass}->{$this->someID};

这个问题可能有答案,但您可能希望看到这些迁移到PHP 7

来源: php.net

如果其他人想要找到深度不详的深层属性,我想出了下面的内容,而不需要遍历所有孩子的所有已知属性。

例如,要查找$ Foo-&gt; Bar-&gt; baz,或$ Foo-&gt; baz,或$ Foo-&gt; Bar-&gt; Baz-&gt; dave,其中$ path是一个类似的字符串'富/酒吧/巴兹'。

public function callModule($pathString, $delimiter = '/'){

    //split the string into an array
    $pathArray = explode($delimiter, $pathString);

    //get the first and last of the array
    $module = array_shift($pathArray);
    $property = array_pop($pathArray);

    //if the array is now empty, we can access simply without a loop
    if(count($pathArray) == 0){
        return $this->{$module}->{$property};
    }

    //we need to go deeper
    //$tmp = $this->Foo
    $tmp = $this->{$module};

    foreach($pathArray as $deeper){
        //re-assign $tmp to be the next level of the object
        // $tmp = $Foo->Bar --- then $tmp = $Bar->baz
        $tmp = $tmp->{$deeper};
    }

    //now we are at the level we need to be and can access the property
    return $tmp->{$property};

}

然后拨打电话:

$propertyString = getXMLAttribute('string'); // '@Foo/Bar/baz'
$propertyString = substr($propertyString, 1);
$moduleCaller = new ModuleCaller();
echo $moduleCaller->callModule($propertyString);

这是我的尝试。它内置了一些常见的“愚蠢”检查,确保您不会尝试设置或获取不可用的成员。

您可以将这些'property_exists'检查分别移动到__set和__get,并直接在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";

?>

这个函数的作用是检查该属性是否存在于他的任何一个孩子的这个类中,如果是,则获取该值,否则返回null。 所以现在属性是可选的和动态的。

/**
 * check if property is defined on this class or any of it's childes and return it
 *
 * @param $property
 *
 * @return bool
 */
private function getIfExist($property)
{
    $value = null;
    $propertiesArray = get_object_vars($this);

    if(array_has($propertiesArray, $property)){
        $value = $propertiesArray[$property];
    }

    return $value;
}

用法:

const CONFIG_FILE_PATH_PROPERTY = 'configFilePath';

$configFilePath = $this->getIfExist(self::CONFIG_FILE_PATH_PROPERTY);
$classname = "myclass";
$obj = new $classname($params);

$variable_name = "my_member_variable";
$val = $obj->$variable_name; //do care about the level(private,public,protected)

$func_name = "myFunction";
$val = $obj->$func_name($parameters);

为什么编辑: 之前:使用eval(邪恶) 之后:根本没有评估。在这种语言中老了。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top