小编Jon*_*tad的帖子

PHP:动态对象名称

可能重复:
按字符串获取PHP类属性

这是我的原始代码:

function generateQuery($type, $language, $options)
{
    // Base type
    $query = $this->Queryparts->print['filter'];

    // Language modifiers
    // Additional options

    return $query;
}
Run Code Online (Sandbox Code Playgroud)

"print"是定义为对象的数组/散列(具有"(对象)"转换).我希望做这样的事情:

    $query = $this->Queryparts->$type['filter'];
Run Code Online (Sandbox Code Playgroud)

使用$ type变量作为对象名称.这可能吗?

php generics scalar dynamic object

6
推荐指数
2
解决办法
9542
查看次数

PHP中的__get资源"不能使用stdClass类型的对象作为数组"

我正在尝试一个关于如何在PHP中存储字符串资源的方法,但我似乎无法让它工作.我对__get函数如何与数组和对象的工作方式有点不确定.

错误消息:"致命错误:不能在第34行的/var/www/html/workspace/srclistv2/Resource.php中使用stdClass类型的对象作为数组"

我究竟做错了什么?

/**
 * Stores the res file-array to be used as a partt of the resource object.
 */
class Resource
{
    var $resource;
    var $storage = array();

    public function __construct($resource)
    {
        $this->resource = $resource;
        $this->load();
    }

    private function load()
    {
        $location = $this->resource . '.php';

        if(file_exists($location))
        {
             require_once $location;
             if(isset($res))
             {
                 $this->storage = (object)$res;
                 unset($res);
             }
        }
    }

    public function __get($root)
    {
        return isset($this->storage[$root]) ? $this->storage[$root] : null;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是名为QueryGenerator.res.php的资源文件:

$res = array(
    'query' => array(
        'print' …
Run Code Online (Sandbox Code Playgroud)

php arrays hash resources object

5
推荐指数
1
解决办法
405
查看次数

标签 统计

object ×2

php ×2

arrays ×1

dynamic ×1

generics ×1

hash ×1

resources ×1

scalar ×1