class a{//主要功能是連接數(shù)據(jù)庫,取出表中字段,將字段定義為子類屬性,想以這種方式實現(xiàn)ActiveRecord
function doQuery($dbname, $table){ $fields = mysql_list_fields($dbname, $table); $num = mysql_num_fields($fields); $keys = array(); for($i=0; $i<$num; $i++){ array_push($keys, mysql_fields_name($fields);
} return $keys;
}
}class b{
function bind($data){ foreach($data as $key=>$value){ $this->$value = null;
} print_r(get_class_vars(get_class($this));
}
}
2 回答

互換的青春
TA貢獻1797條經(jīng)驗 獲得超6個贊
get_class_vars — Get the default properties of the class
動態(tài)定義的屬性是不會被這個函數(shù)打出來的 ...
如果你需要列出一個類的所有屬性 ... 一段小技巧可以幫助到你 ...
<?phpclass b { public function bind( $data ) { foreach ( $data as $key => $value ) $this->$value = null; /* just make an array to output like you want ... */ $ret = []; /* no need to do type conversion here ... php will done for us ... */ foreach( $this as $key => $null_of_course ) $ret[] = $key; /* miracle time ... */ print_r( $ret ); } } (new b)->bind( [ 'aa' => 'bb', 'cc' => 'dd' ] );
或者更簡單的寫法 ... 需要手動轉(zhuǎn)化一下類型 ...
$ret = array_keys( (array) $this );

婷婷同學(xué)_
TA貢獻1844條經(jīng)驗 獲得超8個贊
public function doQuery($table){ $fields = mysql_list_fields($this->dbName, $table); $keys = array(); $num = mysql_num_fields($fields); for ($i=0; $i < $num; $i++) { array_push($keys, mysql_field_name($fields, $i)); } foreach ($keys as $key => $value) { $this->$value = $value; } print_r($keys); Echo '<pre>'; print_r(get_class_vars(get_class($this))); }
- 2 回答
- 0 關(guān)注
- 159 瀏覽
添加回答
舉報
0/150
提交
取消