<bdo id='qtWRx'></bdo><ul id='qtWRx'></ul>

    1. <tfoot id='qtWRx'></tfoot>
      <i id='qtWRx'><tr id='qtWRx'><dt id='qtWRx'><q id='qtWRx'><span id='qtWRx'><b id='qtWRx'><form id='qtWRx'><ins id='qtWRx'></ins><ul id='qtWRx'></ul><sub id='qtWRx'></sub></form><legend id='qtWRx'></legend><bdo id='qtWRx'><pre id='qtWRx'><center id='qtWRx'></center></pre></bdo></b><th id='qtWRx'></th></span></q></dt></tr></i><div id='qtWRx'><tfoot id='qtWRx'></tfoot><dl id='qtWRx'><fieldset id='qtWRx'></fieldset></dl></div>

      <small id='qtWRx'></small><noframes id='qtWRx'>

      <legend id='qtWRx'><style id='qtWRx'><dir id='qtWRx'><q id='qtWRx'></q></dir></style></legend>
    2. PHP - 作为对象的关联数组

      时间:2024-05-11

          <tbody id='hh0r5'></tbody>

      1. <tfoot id='hh0r5'></tfoot>

        1. <small id='hh0r5'></small><noframes id='hh0r5'>

              <i id='hh0r5'><tr id='hh0r5'><dt id='hh0r5'><q id='hh0r5'><span id='hh0r5'><b id='hh0r5'><form id='hh0r5'><ins id='hh0r5'></ins><ul id='hh0r5'></ul><sub id='hh0r5'></sub></form><legend id='hh0r5'></legend><bdo id='hh0r5'><pre id='hh0r5'><center id='hh0r5'></center></pre></bdo></b><th id='hh0r5'></th></span></q></dt></tr></i><div id='hh0r5'><tfoot id='hh0r5'></tfoot><dl id='hh0r5'><fieldset id='hh0r5'></fieldset></dl></div>
                <bdo id='hh0r5'></bdo><ul id='hh0r5'></ul>
                <legend id='hh0r5'><style id='hh0r5'><dir id='hh0r5'><q id='hh0r5'></q></dir></style></legend>
                本文介绍了PHP - 作为对象的关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

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

                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:

                $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?

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

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

                推荐答案

                投吧:

                $user = (object)$user;
                

                当然,还有其他更灵活的解决方案,例如创建一个实现 数组访问:

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

                $user = new User(); // implements ArrayAccess
                
                echo $user['name'];
                // could be the same as...
                echo $user->name;
                

                这篇关于PHP - 作为对象的关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 Silex 框架中使用 YAML 配置文件 下一篇:使用 yaml 路由描述时,如何在运行时获取 Symfony2 中的路由名称?

                相关文章

                <small id='xVzwg'></small><noframes id='xVzwg'>

                <i id='xVzwg'><tr id='xVzwg'><dt id='xVzwg'><q id='xVzwg'><span id='xVzwg'><b id='xVzwg'><form id='xVzwg'><ins id='xVzwg'></ins><ul id='xVzwg'></ul><sub id='xVzwg'></sub></form><legend id='xVzwg'></legend><bdo id='xVzwg'><pre id='xVzwg'><center id='xVzwg'></center></pre></bdo></b><th id='xVzwg'></th></span></q></dt></tr></i><div id='xVzwg'><tfoot id='xVzwg'></tfoot><dl id='xVzwg'><fieldset id='xVzwg'></fieldset></dl></div>
              • <tfoot id='xVzwg'></tfoot>
                <legend id='xVzwg'><style id='xVzwg'><dir id='xVzwg'><q id='xVzwg'></q></dir></style></legend>
                • <bdo id='xVzwg'></bdo><ul id='xVzwg'></ul>