标签:哈哈 判断 cheng 封装 extends lin 错误 关系 内容
前 言
OOP
学习了好久的PHP,今天来总结一下PHP中的重要成员OOP
1 面向过程&面向对象 |
2 面向对象 |
3 类的声明与实例化 |
4 构造函数/析构函数 |
6 魔术方法 |
7 重点来了--php中的封装 |
1 class Person{ 2 private $name; 3 private $age; 4 private $sex; 5 public function __construct($name,$age,$sex){ 6 $this->name=$name; 7 $this->age=$age; 8 $this->sex=$sex; 9 } 10 function __isset($key){ 11 return isset($this->$key); 12 } 13 function __unset($name){ 14 if($name=="name"){ 15 echo "<span style=‘color:red;‘>删除不掉name</span><br>"; 16 return; 17 } 18 unset($this->$name); 19 return; 20 } 21 function __set($key,$value){ 22 if($key=="name"){ 23 $this->$key=$value."这是set出的。"; 24 return; 25 } 26 $this->$key=$value; 27 } 28 function setAge($age){ 29 if($age<=120&&$age>=0){ 30 $this->age =$age; 31 }else{ 32 error_log("年龄设置有误"); 33 } 34 } 35 function getAge($age){ 36 return "但是我永远18岁,哈哈哈哈哈哈。" ; 37 } 38 function __get($key){ 39 return $this->$key."hahahah"; 40 } 41 private function formateName(){ 42 return "我叫{$this->name},我今年{$this->age}岁了!<br>"; 43 } 44 public function say(){ 45 echo $this->formateName(); 46 } 47 } 48 $zhangsan = new Person("张三丰",14,"男"); 49 //$zhangsan->setAge(99); 50 //$zhangsan->say(); 51 //echo $zhangsan->getAge("88"); 52 //$zhangsan->name="李四"; 53 // $zhangsan->name="李四"; 54 echo $zhangsan->name; 55 echo $zhangsan->age; 56 var_dump(isset($zhangsan->age)); 57 unset($zhangsan->age);
8 重点来了--php中的继承 |
1 class Person{ 2 public $name; 3 public $sex; 4 5 6 7 public function __construct($name,$sex){ 8 $this->name=$name; 9 $this->sex=$sex; 10 } 11 12 public function say(){ 13 echo "我叫{$this->name},我是{$this->sex}生! <br> "; 14 } 15 } 16 17 class Student extends Person{ 18 public $school; 19 public function __construct($school){ 20 $this->school=$school; 21 } 22 23 public function say(){ 24 parent::say(); 25 echo "我叫{$this->name},我是{$this->sex}生! <br> "; 26 } 27 function say1(){ 28 echo "今天下雨了,啦啦啦啦啦。<br>"; 29 } 30 } 31 32 $lalala = new Student("每天学习"); 33 $lalala->name= "lalala"; 34 $lalala->sex="不详"; 35 $lalala->say(); 36 $lalala->say1();
9 重点来了--php中的继承 |
1 abstract class Person{ 2 3 abstract function say(); 4 function say1(){ 5 echo "我是Say1"; 6 } 7 } 8 class Chinese extends Person{ 9 function say(){ 10 echo "我是一个中国人<br>"; 11 } 12 } 13 class English extends Person{ 14 function say(){ 15 echo "I‘am a English"; 16 } 17 } 18 19 /*$a = new Chinese(); 20 $a->say(); 21 22 $b = new English(); 23 $b->say(); */ 24 25 function func(Person $p){ 26 $p->say(); 27 }
学习时候的笔记,可能会有一些错误的地方,欢迎各位的批评指点。
反思,复盘,每天收获一点---------------------期待更好的自己
>>>---PHP中的OOP-->面对过程与面对对象基础概念与内容--(封装、继承、多态)
标签:哈哈 判断 cheng 封装 extends lin 错误 关系 内容
原文地址:http://www.cnblogs.com/zhuanzhibukaixin/p/6979686.html