• <legend id='QHGw1'><style id='QHGw1'><dir id='QHGw1'><q id='QHGw1'></q></dir></style></legend>

  • <tfoot id='QHGw1'></tfoot>

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

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

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

        PHP 反射类.如何获取属性的值?

        时间:2023-10-13
        <i id='Xbbp5'><tr id='Xbbp5'><dt id='Xbbp5'><q id='Xbbp5'><span id='Xbbp5'><b id='Xbbp5'><form id='Xbbp5'><ins id='Xbbp5'></ins><ul id='Xbbp5'></ul><sub id='Xbbp5'></sub></form><legend id='Xbbp5'></legend><bdo id='Xbbp5'><pre id='Xbbp5'><center id='Xbbp5'></center></pre></bdo></b><th id='Xbbp5'></th></span></q></dt></tr></i><div id='Xbbp5'><tfoot id='Xbbp5'></tfoot><dl id='Xbbp5'><fieldset id='Xbbp5'></fieldset></dl></div>

                <tbody id='Xbbp5'></tbody>

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

            • <legend id='Xbbp5'><style id='Xbbp5'><dir id='Xbbp5'><q id='Xbbp5'></q></dir></style></legend>
                <tfoot id='Xbbp5'></tfoot>

                • <bdo id='Xbbp5'></bdo><ul id='Xbbp5'></ul>
                • 本文介绍了PHP 反射类.如何获取属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 PHP 中使用反射类,但我不知道如何获取反射实例中的属性值.有可能吗?

                  I'm using the reflection class in PHP, but I'm with no clues on how to get the values of the properties in the reflection instance. It is possible?

                  代码:

                  <?php
                  
                  class teste {
                  
                      public $name;
                      public $age;
                  
                  }
                  
                  $t = new teste();
                  $t->name = 'John';
                  $t->age = '23';
                  
                  $api = new ReflectionClass($t);
                  
                  foreach($api->getProperties() as $propertie)
                  {
                      print $propertie->getName() . "
                  ";
                  }
                  
                  ?>
                  

                  如何获取 foreach 循环内的属性值?

                  How can I get the propertie values inside the foreach loop?

                  此致,

                  推荐答案

                  怎么样

                  • ReflectionProperty::getValue - 获取属性值.

                  就你而言:

                  foreach ($api->getProperties() as $propertie)
                  {
                      print $propertie->getName() . "
                  ";
                      print $propertie->getValue($t);
                  }
                  

                  顺便说一句,因为你的对象只有公共成员,你也可以迭代它直接

                  On a sidenote, since your object has only public members, you could just as well iterate it directly

                  foreach ($t as $propertie => $value)
                  {
                      print $propertie . "
                  ";
                      print $value;
                  }
                  

                  或使用 get_object_vars 获取它们一个数组.

                  or fetch them with get_object_vars into an array.

                  这篇关于PHP 反射类.如何获取属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 htaccess 重写/重定向,以便单个 PHP 文件可以根据 GET/POST 变量显示数据 下一篇:在 PHP 中获取和删除数组的第一个元素

                  相关文章

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

                • <small id='tmR0p'></small><noframes id='tmR0p'>

                  1. <legend id='tmR0p'><style id='tmR0p'><dir id='tmR0p'><q id='tmR0p'></q></dir></style></legend>

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