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

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

      • <bdo id='IujN1'></bdo><ul id='IujN1'></ul>
      <tfoot id='IujN1'></tfoot>
    2. 如何访问在 php 中命名为变量(点表示法)的深层对象属性?

      How can I access a deep object property named as a variable (dot notation) in php?(如何访问在 php 中命名为变量(点表示法)的深层对象属性?)

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

          <bdo id='Htfrz'></bdo><ul id='Htfrz'></ul>
          <tfoot id='Htfrz'></tfoot>
              <tbody id='Htfrz'></tbody>

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

              • <legend id='Htfrz'><style id='Htfrz'><dir id='Htfrz'><q id='Htfrz'></q></dir></style></legend>
              • 本文介绍了如何访问在 php 中命名为变量(点表示法)的深层对象属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有很多与此类似的问题,但这里略有不同,因为它是关于深度对象属性访问的,而不仅仅是一层深度.

                There are many questions similar to this, however this is slightly different since it's about deep object property access, not just one level of depth.

                假设我有一个包含字符串 foo.bar 的变量.

                Let's say I have a variable containing the string foo.bar.

                $user = new User();
                $user->foo = new Foo();
                $user->foo->bar = "Hello World";
                
                $variable = "foo.bar"
                

                我想通过使用 $variable 来回显 $user->foo->bar:

                I would like to echo $user->foo->bar by making use of $variable:

                echo $user->foo->bar
                

                这是我迄今为止尝试过但没有成功的方法(它说 NULL):

                This is what I have tried so far but with no success (it says NULL):

                $value = str_replace(".", "->", $value);
                echo $user->{$value};
                

                推荐答案

                使用可变属性符号($o->$p减少对象路径非常容易代码>):

                It is very easy to reduce the object path using variable property notation ($o->$p):

                $path = 'foo.bar';
                echo array_reduce(explode('.', $path), function ($o, $p) { return $o->$p; }, $user);
                

                这很容易变成一个小的辅助函数.

                This could easily be turned into a small helper function.

                这篇关于如何访问在 php 中命名为变量(点表示法)的深层对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                How to parse django style template tags(如何解析 django 样式模板标签)
                What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                php in background exec() function(php 后台 exec() 函数)

                    <tbody id='1CldV'></tbody>

                    <legend id='1CldV'><style id='1CldV'><dir id='1CldV'><q id='1CldV'></q></dir></style></legend>
                      <bdo id='1CldV'></bdo><ul id='1CldV'></ul>

                      <small id='1CldV'></small><noframes id='1CldV'>

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