类中属性返回形式(对象,关联数组,索引数组)
2014-06-20 21:59
741 查看
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);
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);
相关文章推荐
- php get_object_vars($object) 返回由对象属性组成的关联数组
- 使用jQuery匹配文档中所有的li元素,返回一个jQuery对象,然后通过数组下标的方式读取jQuery集合中第1个DOM元素,此时返回的是DOM对象,然后调用DOM属性innerHTML,读取该元素 包含的文本信息
- constructor 属性返回变量或对象的构造函数。判断是否为日期,数组的例子
- 写一个 function,它遍历一个对象数组(第一个参数)并返回一个包含相匹配的属性-值对(第二个参数)的所有对象的数组。
- 对象和函数通过数组下表的形式来访问自身的属性
- 遍历一个对象数组,返回一个包含相匹配的属性-值对的所有对象的数组
- php将结果集匹配成索引数组、关联数组、对象的区别&&location对象
- 有关 关联数组、索引数组、对象在Smarty中的应用
- PHP索引数组转换成json是数组,php关联数组转换成json是对象。
- 将非托管代码返回的一个object 对象转换为 string 数组
- C#返回数组对象
- 使用mysql_fetch_row()以数组的形式返回查询结果
- 根据表单的name(名字)数组,返回对应的Map对象。Map对象的key是表单的name,Map对象的value是表单的value
- oracle 存储过程返回对象数组的方法
- Delphi对象的default数组属性
- 筛选关联对象 :外键值 VS 导航属性
- 以交错数组形式返回指定年份各月的工作日的方法
- 【整理】.Net Framework3.5之隐式类型的本地变量和数组、对象初始值设定项、集合初始值设定项、自动实现的属性和匿名类型
- js以对象为索引的关联数组
- Javascript数组对象的方法和属性