使用Object创建另一个具有附加属性的Object,php

时间:2016-07-30 20:21:27

标签: php oop

这是我的PHP代码:

<?php
tree($Comments, 0, 0);
$var = -1;
function tree($Comments, $parent_id = 0, $level=0, $c=0) {
  global $var;
  foreach($Comments as $Comment) {
      if($Comment['parent_id'] == $parent_id) {
        //create a next instance of the new object with values of this current instance of the old object, like $NewObjectInstance['parent_id']=$Comment['parent_id'] and so on, in this way the new object will be ordered as i want
        If ($level > $var)  $var++; else {  
                for ($i = $var-$level+1; $i>0; $i--) { if ($c < 0) // the value of the new fancy property of new object instance(let's say $NewObjectInstance->design_level) will be incremented by each interation if (if) doesn't work it will be 0
                  else $c--;  };   
                $var=$level; 
                   }; 
        tree($Comments, $Comment['id'], $level+1,$c);
        }
  };
};
?>

我有$ Comments对象,其中包含Table(properties-values)的值:

id
parent_id
id_theme
user
text
upVotes
downVotes

我的想法是在对象的帮助下使用上面的递归函数创建,我自己使用相同的属性和值排序的另一个对象,以及添加的特殊属性(design_level)。但我是对象中的新手而且我被卡住了,因为我只是被程序编程所困,任何想法如何制作这个?

0 个答案:

没有答案