<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 中命名为变量(点表示法)的深层对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

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