The video "Li Yanhui PHP Video Tutorial Season 2" is based on "Li Yanhui PHP Video Tutorial Season 1" to deepen the learning of PHP. This video introduces object-oriented thinking, practical membership registration and cms management system Explain in detail, through the study of these knowledge points, I believe everyone's strength will be significantly improved
Course playback address: http://www. php .cn/course/398.html
The teacher’s teaching style:
The teacher’s lectures are vivid, witty, witty, and touching. . A vivid metaphor is like the finishing touch, opening the door to wisdom for students; a well-placed humor brings a knowing smile to students, like drinking a glass of mellow wine, giving people aftertaste and nostalgia; a philosopher's aphorisms, cultural references Proverbs are interspersed from time to time in the narration, giving people thinking and warning.
The more difficult point in this video is object-oriented:
Encapsulation is one of the three major characteristics of object-oriented programming. Encapsulation is to encapsulate the object Properties and services are combined into an independent and identical unit, and the internal details of the object are hidden as much as possible. It contains two meanings:
1. Combine all the properties and all services of the object to form an indivisible Independent units (i.e. objects).
2. Information hiding, that is, hiding the internal details of the object as much as possible, forming a boundary (or forming a barrier) to the outside world, and only retaining a limited external interface to connect it with the outside world.
The reflection of the principle of encapsulation in software is: it requires that parts other than the object cannot access the internal data (properties) of the object at will, thereby effectively avoiding "cross-infection" of external errors and making it Software errors can be localized, greatly reducing the difficulty of error detection and troubleshooting.
Let’s use an example to illustrate. Suppose a person’s object has attributes such as age and salary. Such personal privacy attributes are not accessible to other people. If you don’t use encapsulation, Then others can get it if they want to know, but if you encapsulate it, others will have no way to obtain the encapsulated attributes. Unless you tell it yourself, others will have no way to get it.
For example, personal computers have a password, and you don’t want others to log in at will and copy and paste it into your computer. Also, for objects like people, the attributes of height and age can only be increased by oneself, and cannot be assigned values arbitrarily by others, etc.
Use the private keyword to encapsulate properties and methods:
Original members:
<?php var $name; //声明人的姓名 var $sex; //声明人的性别 var $age; //声明人的年龄 function run(){……}
Change to encapsulated form:
<?php private $name; //把人的姓名使用private关键字进行封装 private $sex; //把人的性别使用private关键字进行封装 private $age; //把人的年龄使用private关键字进行封装 private function run(){……} //把人的走路方法使用private关键字进行封装
None Add any access control, the default is public and can be accessed from anywhere.
<?php //这个人可以说话的方法, 说出自己的私有属性,在这里也可以访问私有方法 function say() { echo "我的名子叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this->age; //在这里也可以访问私有方法 //$this->run(); }
Because the member method say() is public, it is okay for us to call the say() method outside the class. Change the above code:
<?php class Person { //下面是人的成员属性 private $name; //人的名子,被private封装上了 private $sex; //人的性别, 被private封装上了 private $age; //人的年龄, 被private封装上了 //定义一个构造方法参数为私有的属性姓名$name、性别$sex和年龄$age进行赋值 function __construct($name, $sex, $age) { //通过构造方法传进来的$name给私有成员属性$this->name赋初使值 $this->name = $name; //通过构造方法传进来的$sex给私有成员属性$this->sex赋初使值 $this->sex = $sex; //通过构造方法传进来的$age给私有成员属性$this->age赋初使值 $this->age = $age; } //这个人可以说话的方法, 说出自己的私有属性,在这里也可以访问私有方法 function say() { echo "我的名子叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this->age; } } //通过构造方法创建3个对象$p1、p2、$p3,分别传入三个不同的实参为姓名、性别和年龄 $p1 = new Person("张三", "男", 20); $p2 = new Person("李四", "女", 30); $p3 = new Person("王五", "男", 40); //下面访问$p1对象中的说话方法 $p1->say(); //下面访问$p2对象中的说话方法 $p2->say(); //下面访问$p3对象中的说话方法 $p3->say(); ?>
Output result:
我的名子叫:张三 性别:男 我的年龄是:20我的名子叫:李四 性别:女 我的年龄是:30我的名子叫:王五 性别:男 我的年龄是:40
The above is the detailed content of Recommended resources for the second season of Li Yanhui's PHP video tutorial. For more information, please follow other related articles on the PHP Chinese website!