类中属性返回形式(对象,关联数组,索引数组)

时间:2014-06-22 23:21:06   收藏:0   阅读:169

class A {

 public $x, $y;

 function __construct($x, $y)  {

  $this->x = $x;

  $this->y = $y;

 }

   function get_value($arr = true)  {

  if($arr == ‘arr‘)   {

     // 类中属性以关联数组形式转换返回

   return get_object_vars($this);

  }else if($arr == ‘obj‘)   {

   //类中属性以对象形式返回

   return $this;

  }else   {

   // 类中属性以索引数组形式转换返回

   return array_values(get_object_vars($this));

  }

 }

}

$aa = new A(100, 200);

$ok = $aa->get_value();

print_r($ok);

类中属性返回形式(对象,关联数组,索引数组),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!