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

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

原文地址:https://www.cnblogs.com/lin3615/p/3800199.html