class Data {
public $name;
public $gender;
public $age;
public function __construct($name,$gender,$age) {
$this->name = $name;
$this->gender = $gender;
$this->age = $age;
}
}
class Test {
public function run() {
$data = [
new Data('张三','男',18),
new Data('李四','男',14),
new Data('王五','男',17),
new Data('大姨妈','女',23),
];
}
private function eachData($data) {
foreach($data as $item) {
echo $item->name.'=>'.$item->gender.'=>'.$item->age."\n";
}
}
}
(new Test)->run();
ログイン後にコピー
上記の例から判断すると、一般的には問題ありませんが、コード
class Data { public $name; public $gender; public $age; public function __construct($name,$gender,$age) { $this->name = $name; $this->gender = $gender; $this->age = $age; } } class Test { public function run() { $data = [ new Data('张三','男',18), new Data('李四','男',14), new Data('王五','男',17), new Data('大姨妈','女',23), ]; } private function eachData($data) { foreach($data as $item) { echo $item->name.'=>'.$item->gender.'=>'.$item->age."\n"; } } } (new Test)->run();
ログイン後にコピー
cho $item->name.'=>'.$item->sex.'=>'.$item->age."\n";
ログイン後にコピー
class Data { public $name; public $gender; public $age; public function __construct($name,$gender,$age) { $this->name = $name; $this->sex = $gender; $this->age = $age; } } class Test { public function run() { $data = [ new Data('张三','男',18), new Data('李四','男',14), new Data('王五','男',17), new Data('大姨妈','女',23), ]; } /** * 遍历输出数据 * @param array $data */ private function eachData($data) { foreach($data as $item) { if($item instanceof Data) { echo $item->name.'=>'.$item->gender.'=>'.$item->age."\n"; } } } } (new Test)->run();
ログイン後にコピー