austindev 2016-03-19
类是我们队一组对象的描述
在php里,每个类的定义都以关键字class开头,后面跟着类名,紧接着一对花括号,里面包含有类成员和方法的定义。如下代码所示
class person{
public $name;
public $gender;
public function say(){
echo $this->name."is ".$this->gender;
}
}<!--more-->接下来就可以产生这个类的实例:
$student = new person(); $student->name="Tome"; $student->gender= "male"; $student->say(); $teacher= new person(); $teacher->name="kati"; $teacher->gender= "female"; $teacher->say();
这段代码则实例化了person类,产生了一个student对象和teacher对象的实例。实际上也就是从抽象到具体的过程。
对类和对象的一些理解:
打印student对象
print_r((array)$student); var_dump($student);
序列化对象
$str = serialize($student);
echo $str;
file_put_contents('store.txt',$str);
输出结果:
0:6:"person":2:{s:4:"name";s:3:"Tom";s:6:"gender";s:4:"mail";}反序列化对象
$str = file_get_contents('store.txt');
$student = unserialize($str);
$student->say();