类是咱们队一组对象的描述php
在php里,每一个类的定义都以关键字class开头,后面跟着类名,紧接着一对花括号,里面包含有类成员和方法的定义。以下代码所示post
class person{ public $name; public $gender; public function say(){ echo $this->name."is ".$this->gender; } }
接下来就能够产生这个类的实例:this
$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对象的实例。实际上也就是从抽象到具体的过程。code
对类和对象的一些理解:对象
打印student对象get
print_r((array)$student); var_dump($student);
序列化对象io
$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";}
反序列化对象function
$str = file_get_contents('store.txt'); $student = unserialize($str); $student->say();
转载自:http://www.9958.pw/post/php_classclass