当前位置:网站首页>PHP parent
PHP parent
2022-07-02 05:49:00 【Jill__ er】
php parent In object-oriented grammar, it represents “ Parent class ”, In essence, it represents the parent class “ class ”, Not the parent “ object ”, Its usage is “parent:: Properties or methods ;”.
keyword | meaning | Use location | Examples of use |
---|---|---|---|
parent: | Represents the parent class ( This class ) | It must be in one way | parent:: Properties or methods ; |
self: | Represents the current class | It must be in one way | self:: Static property or method |
$this: | Represents the object that calls the current method | It must be in one way | $this-> Instance property or method ; |
<?php
class Person{
protected $name;
protected $sex;
protected $age;
function __construct($name="", $sex=" male ", $age=1){
$this->name = $name;
$this->sex = $sex;
$this->age = $age;
}
function say(){
echo " My name : ".$this->name.", Gender : ".$this->sex,", Age : ".$this->age;
}
}
class Student extends Person{
private $school; // Add a school in the student class school Member properties of .
// Override constructor in parent class , Add a school attribute to the parameter list , Used to create objects and initialize member properties
function __construct($name="", $sex=" male ", $age=1, $school=""){
parent::__construct($name, $sex, $age);
$this->school = $school;
}
function stydy(){
echo $this-name." is ".$this->school." Study ";
}
function say(){
parent::say(); // Call the method in the parent class that is overridden by this method
echo " stay ".$this->school." School ";
}
}
$student = new Student(" Zhang San ", " Woman ", "23", "edu");// Create a student object , And pass it to a school name parameter
$student->say();// Call the speaking method in the student class that overrides the parent class
/** The result is : My name : Zhang San , Gender : Woman , Age : 23 stay edu School */
// Be careful : The access permission of the method overridden in the subclass must not be lower than that of the method overridden by the parent class .
// For example, the access permission of the method in the parent class is protected, Then the access permission of the method overridden in the subclass is protected perhaps public
?>
边栏推荐
- 2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions
- Financial portal related information
- 测试 - 用例篇
- 软件测试 - 概念篇
- 3D 打印机 G 代码命令:完整列表和教程
- Fabric. JS compact JSON
- Zzuli:1061 sequential output of digits
- php读文件(读取json文件,转换为数组)
- Zzuli:1066 character classification statistics
- [golang syntax] be careful with the copy of slices
猜你喜欢
Opencv LBP features
2022-2-14 learning xiangniuke project - Section 6 displays login information
Centos8 installation mysql8.0.22 tutorial
Visual studio import
Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import
Installation du tutoriel MySQL 8.0.22 par centos8
RGB infinite cube (advanced version)
Record sentry's path of stepping on the pit
在线音乐播放器app
死磕大屏UI,FineReport开发日记
随机推荐
Principle and implementation of parallax effect
How to write good code - Defensive Programming Guide
Fabric. JS iText sets the color and background color of the specified text
c语言中的几个关键字
H5 jump applet
[Chongqing Guangdong education] selected reading reference materials of British and American literature of Nanyang Normal University
软件测试 - 概念篇
js判断移动端还是pc端
kmp思想及模板代码
Go language web development is very simple: use templates to separate views from logic
Vscode paste image plugin saves image path settings
idea开发工具常用的插件合集汇总
RGB 无限立方体(高级版)
Fabric. JS background is not affected by viewport transformation
Fabric. JS right click menu
Basic use of form
“簡單”的無限魔方
[leetcode] day92 container with the most water
Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan
金融门户相关信息