类是我们队一组对象的描述
在php里,每个类的定义都以关键字class开头,后面跟着类名,紧接着一对花括号,里面包含有类成员和方法的定义。如下代码所示
class person{ public $name; public $gender; public function say(){ echo $this->name."is ".$this->gender; }}接下来就可以产生这个类的实例:
$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对象的实例。实际上也就是从抽象到具体的过程。
对类和对象的一些理解:
立即学习“PHP免费学习笔记(深入)”;
打印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();转载自:http://www.9958.pw/post/php_class
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号