PHP - 关联数组作为对象[重复]

时间:2022-09-14 21:33:07

Possible Duplicate:
Convert Array to Object PHP

可能重复:将数组转换为对象PHP

I'm creating a simple PHP application and I would like to use YAML files as a data storage. I will get the data as an associative array, with this structure for example:

我正在创建一个简单的PHP应用程序,我想使用YAML文件作为数据存储。我将获取数据作为关联数组,例如:

$user = array('username' => 'martin', 'md5password' => '5d41402abc4b2a76b9719d911017c592')

However, I would like to extend the associative array with some functions and use the -> operator, so I can write something like this:

但是,我想用一些函数扩展关联数组并使用 - >运算符,所以我可以写这样的东西:

$user->username = 'martin';  // sets $user['username']
$user->setPassword('hello'); // writes md5 of 'hello' to $user['md5password']
$user->save();               // saves the data back to the file

Is there a good way to do this without a class definition?

没有类定义,有没有一种好方法可以做到这一点?

Basically, I would like to have JavaScript style objects in PHP :)

基本上,我想在PHP中使用JavaScript样式对象:)

2 个解决方案

#1


28  

Just cast it:

只是施展它:

$user = (object)$user;

Of course, there are other, more flexible solutions like creating a class that implements ArrayAccess:

当然,还有其他更灵活的解决方案,比如创建一个实现ArrayAccess的类:

$user = new User(); // implements ArrayAccess

echo $user['name'];
// could be the same as...
echo $user->name;

#2


5  

Literally just make a $class = new stdClass; and iterate and reassign. Be aware this is only one level deep, just like typecasting. You would have to write a recursive iterator to get it all. From what I remember Kohana 2/3 has to_object() you can probably use.

字面上只需要一个$ class = new stdClass;并迭代并重新分配。请注意,这只是一个层次,就像类型转换一样。你必须编写一个递归迭代器来实现它。从我记得Kohana 2/3有to_object()你可以使用。

Found it:

找到了:

class Arr extends Kohana_Arr {

    public static function to_object(array $array, $class = 'stdClass')
    {
            $object = new $class;
            foreach ($array as $key => $value)
            {
                    if (is_array($value))
                    {
                    // Convert the array to an object
                            $value = arr::to_object($value, $class);
                    }
                    // Add the value to the object
                    $object->{$key} = $value;
            }
            return $object;
    }

#1


28  

Just cast it:

只是施展它:

$user = (object)$user;

Of course, there are other, more flexible solutions like creating a class that implements ArrayAccess:

当然,还有其他更灵活的解决方案,比如创建一个实现ArrayAccess的类:

$user = new User(); // implements ArrayAccess

echo $user['name'];
// could be the same as...
echo $user->name;

#2


5  

Literally just make a $class = new stdClass; and iterate and reassign. Be aware this is only one level deep, just like typecasting. You would have to write a recursive iterator to get it all. From what I remember Kohana 2/3 has to_object() you can probably use.

字面上只需要一个$ class = new stdClass;并迭代并重新分配。请注意,这只是一个层次,就像类型转换一样。你必须编写一个递归迭代器来实现它。从我记得Kohana 2/3有to_object()你可以使用。

Found it:

找到了:

class Arr extends Kohana_Arr {

    public static function to_object(array $array, $class = 'stdClass')
    {
            $object = new $class;
            foreach ($array as $key => $value)
            {
                    if (is_array($value))
                    {
                    // Convert the array to an object
                            $value = arr::to_object($value, $class);
                    }
                    // Add the value to the object
                    $object->{$key} = $value;
            }
            return $object;
    }