当前位置:网站首页>PHP extensions
PHP extensions
2022-07-02 05:49:00 【Jill__ er】
Class inheritance should pass extends To achieve
class Subclass name extends Parent class name {…………}
Subclasses access the parent class public Member method
- Subclasses can inherit the constructor of the parent class , When subclasses are instantiated ,php Will first find the constructor in the subclass .
- If subclass has its own constructor ,php The constructor in the subclass will be called first .
- When a subclass does not have its own constructor ,php Will call the constructor in the parent class .
<?php
class Website{
public $name, $url, $title;
public function __construct(){
echo '-- Constructor in base class --';
}
public function demo(){
echo '-- Member methods in the base class ';
}
}
class ClassOne extends Website{
}
class ClassTwo extends Website{
public function __construct(){
echo '-- Constructors in subclasses --';
}
}
$object = new ClassOne();
$object->demo();
/* The result is :-- Constructor in base class --
-- Member methods in the base class */
$object2 = new ClassTwo();
$object2->demo();
/* The result is :-- Constructors in subclasses --
-- Member methods in the base class */
?>
Subclasses access the parent class protected Member method
- Some classes inherit properties , Don't want to be accessed outside the class , This member is declared a protected member , use protected modification .
- Protected members cannot be accessed outside the class , But it can be accessed inside subclasses .
- That is to say, we can set a function in the subclass , To access this protected member in the parent class .
<?php
class Website{
public $name, $url, $title;
public function __construct(){
echo '-- Constructor in base class --';
}
protected function demo(){
echo '-- Member methods in the base class ';
}
}
class ClassOne extends Website{
}
class ClassTwo extends Website{
public function __construct(){
echo '-- Constructors in subclasses --';
}
public function test(){
$this->demo();
}
}
$object = new ClassOne(); /* The result is :-- Constructor in base class --*/
//$object->demo(); // Call the parent class in the subclass to use protected The decorated member method will report an error
$object2 = new ClassTwo();
$object2->test();
/* The result is :-- Constructors in subclasses --
-- Member methods in the base class - */
?>
Subclasses access the parent class private Member method
- private Decorate private members
- Private members in the parent class will not be inherited by subclasses .
- Therefore, you cannot be asked by subclasses .
<?php
class Website{
public $name, $url, $title;
public function __construct(){
echo '-- Constructor in base class --';
}
private function demo(){
echo '-- Member methods in the base class ';
}
}
class ClassTwo extends Website{
public function test(){
$this->demo();
}
}
$object2 = new ClassTwo();
$object2->test();
/* The result is : -- Constructor in base class --
PHP Fatal error: Uncaught Error: Call to private method Website::demo() from context 'ClassTwo' in 5_php_extends_private.php:14*/
/* Cause of error : Call in the subclass and use in the parent class private Keyword decorated members , The program will report an error and terminate .*/
?>
边栏推荐
- Fabric. JS iText superscript and subscript
- KMP idea and template code
- Fabric. JS right click menu
- [Chongqing Guangdong education] selected reading reference materials of British and American literature of Nanyang Normal University
- 2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions
- Fabric. JS free draw rectangle
- Thread pool overview
- Visual Studio导入
- "Original, excellent and vulgar" in operation and maintenance work
- 2022-2-14 learning xiangniuke project - Section 7 account setting
猜你喜欢
随机推荐
vite如何兼容低版本浏览器
php内的addChild()、addAttribute()函数
1037 Magic Coupon
File contains vulnerability (I)
Appnuim environment configuration and basic knowledge
Pytorch Chinese document
Pytorch Basics
Grbl software: basic knowledge of simple explanation
LCD之MIPI协议的一些说明
Straighten elements (with transition animation)
Fabric. JS activation input box
File contains vulnerabilities (II)
all3dp.com网站中全部Arduino项目(2022.7.1)
Visual studio import
Record sentry's path of stepping on the pit
数据挖掘方向研究生常用网站
Online music player app
OLED12864 液晶屏
Visual Studio導入
Fabric. JS iText sets the color and background color of the specified text









