当前位置:网站首页>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
- 2022-2-14 learning xiangniuke project - Section 7 account setting
- Zzuli:1067 faulty odometer
- [personal test] copy and paste code between VirtualBox virtual machine and local
- Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
- Brew install * failed, solution
- php父类(parent)
- GRBL 软件:简单解释的基础知识
- Cube magique infini "simple"
- Zzuli:1069 learn from classmate Z
猜你喜欢
“简单”的无限魔方
Straighten elements (with transition animation)
mysql事务和隔离级别
ThreadLocal memory leak
Stick to the big screen UI, finereport development diary
软件测试基础篇
【论文翻译】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
我所理解的DRM显示框架
idea開發工具常用的插件合集匯總
Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
随机推荐
Vscode paste image plugin saves image path settings
centos8安裝mysql8.0.22教程
Zzuli:1064 encrypted characters
线程池概述
Matplotlib double Y axis + adjust legend position
Oled12864 LCD screen
1037 Magic Coupon
Fabric. JS 3 APIs to set canvas width and height
青训营--数据库实操项目
Ubuntu 20.04 installing mysql8
KMP idea and template code
Record sentry's path of stepping on the pit
idea開發工具常用的插件合集匯總
Zzuli:1065 count the number of numeric characters
Basic use of form
Reading notes of cgnf: conditional graph neural fields
Fabric. JS round brush
Centos8 installation mysql8.0.22 tutorial
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
【技术随记-08】