深入理解对象2
2018年8月8日对象是变量的一种
对象和数组内容上一模一样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php class Person { public $name; public $gender; public function say() { echo $this->name.' is '. $this->gender.' '; } } $student = new Person; $student->name = 'Tom'; $student->gender = 'male'; $student->say(); $teacher = new Person; $teacher->name = 'Kate'; $teacher->gender = 'female'; $teacher->say(); print_r((array)$student); $str = serialize($student); //就是将一个变量所代表的 “内存数据”转换为“字符串”的形式,并持久保存在硬盘(写入文件中保存)上的一种做法,即,把“内存数据”转换为“字符串”然后保存到文件中; echo $str; file_put_contents('store.txt', $str); //file_put_contents — 将一个字符串写入文件清除写入 $hehe = unserialize($str); $hehe->say(); $student_arr = array('name'=>'Rom','gender'=>'male'); echo PHP_EOL; //终端模式下换行 echo serialize($student_arr); |