💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
我们看一个demo ``` <?php /** * User: 三千 * Date: 16/3/15 * Time: 下午5:18 */ class person{ public $name; public $gender; public function say(){ echo $this->name,"\t is ",$this->gender,"\r\n"; } } class family{ public $people; public $location; public function __construct($p,$loc){ $this->people = $p; $this->location = $loc; } } $student = new person(); $student->name = "Tom"; $student->gender = "male"; $student->say(); $tom = new family($student,"peking"); echo serialize($student); $student_arr = array("name"=>"Tom","gender"=>"male"); echo "\n"; echo serialize($student_arr); print_r($tom); echo "\n"; echo serialize($tom); ``` 返回结果 ``` Tom is male O:6:"person":2:{s:4:"name";s:3:"Tom";s:6:"gender";s:4:"male";} a:2:{s:4:"name";s:3:"Tom";s:6:"gender";s:4:"male";}family Object ( [people] => person Object ( [name] => Tom [gender] => male ) [location] => peking ) O:6:"family":2:{s:6:"people";O:6:"person":2:{s:4:"name";s:3:"Tom";s:6:"gender";s:4:"male";}s:8:"location";s:6:"peking";}% ``` 序列化后的对象会附带所属的类名,这个类名保证此对象能够执行类的方法时,可以正确地找到方法所在的代码空间。另外,当一个对象的实例变量引用其他对象时,序列化该对象时也会对引用对象进行序列化。 对象和类的概念及两者之间的关系: - 类是定义一系列属性和操作的模板,而对象则把属性进行具体化,然后交给类处理 - 对象就是数据,对象本身不包含方法。但是对象有一个“指针”指向一个类,这个类里可以有方法。 - 方法描述不同属性所导致的不同表现。 - 类和对象是不可分割的,有对象就必定有一个类和其对应,否则这个对象也就成了没有亲人和孩子