当前位置:网站首页>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
?>
边栏推荐
- File contains vulnerabilities (II)
- Ubuntu 20.04 installing mysql8
- [paper translation] gcnet: non local networks meet squeeze exception networks and beyond
- Cambrian was reduced by Paleozoic venture capital and Zhike shengxun: a total of more than 700million cash
- 青训营--数据库实操项目
- Fabric. JS activation input box
- Test case
- 2022-2-15 learning xiangniuke project - Section 8 check login status
- Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
- Zzuli:1062 greatest common divisor
猜你喜欢

Youth training camp -- database operation project

vite如何兼容低版本浏览器

A collection of commonly used plug-ins for idea development tools

The Hong Kong Stock Exchange learned from US stocks and pushed spac: the follow-up of many PE companies could not hide the embarrassment of the world's worst stock market

Fabric. JS upload local image to canvas background

Fabric. JS centered element

Résumé de la collection de plug - ins couramment utilisée dans les outils de développement idea

《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记

"Simple" infinite magic cube

Lantern Festival gift - plant vs zombie game (realized by Matlab)
随机推荐
[Chongqing Guangdong education] selected reading reference materials of British and American literature of Nanyang Normal University
h5跳小程序
php父类(parent)
Fabric. JS background is not affected by viewport transformation
Taskbar explicit / implicit toggle function
Visual Studio导入
c语言中的几个关键字
3D 打印机 G 代码命令:完整列表和教程
Alibaba: open source and self-developed liquid cooling data center technology
Practice C language advanced address book design
H5 jump applet
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
Sliding window on the learning road
Huawei Hongmeng OS, is it OK?
XSS basic content learning (continuous update)
Fabric. JS 3 APIs to set canvas width and height
软件测试基础篇
Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
Applet jumps to official account
[technical notes-08]