深入理解对象
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 |
<?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(); |
1 2 3 4 5 6 |
Tom is male Kate is female Array ( [name] => Tom [gender] => male ) O:6:"Person":2:{s:4:"name";s:3:"Tom";s:6:"gender";s:4:"male";}Tom is male |
从上面的代码可以看出,对象就是内存中的,一段数据,通过序列化将他从内存中取出,保存到,文本中,然后反序列化将他存到内存中,可以接着操作这个对象