doushi4864 2011-11-29 09:43 采纳率: 100%
浏览 23
已采纳

动态发现/处理PHP对象

Is it possible to dynamically discover the properties of a PHP object? I have an object I want to strip and return it as a fully filled stdClass object, therefore I need to get rid of some sublevel - internal - objecttypes used in the input.

My guess is I need it to be recursive, since properties of objects in the source-object can contain objects, and so on.

Any suggestions, I'm kinda stuck? I've tried fiddling with the reflection-class, get_object_vars and casting the object to an array. All without any success to be honest..

  • 写回答

2条回答 默认 最新

  • dongmi6102 2011-11-29 10:00
    关注

    tested and this seems to work:

    <?php
    
    class myobj {private $privatevar = 'private'; public $hello = 'hellooo';}
    
    $obj = (object)array('one' => 1, 'two' => (object)array('sub' => (object)(array('three' => 3, 'obj' => new myobj))));
    
    var_dump($obj);
    
    echo "
    ", json_encode($obj), "
    ";
    
    $recursive_public_vars = json_decode(json_encode($obj));
    
    var_dump($recursive_public_vars);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错