


Class is an important concept in Object-oriented, and encapsulation and inheritance are important features of object-oriented. In this article, we will focus on the two characteristics of encapsulation and inheritance, and give some examples for your reference.
Encapsulation
Encapsulate member methods and member properties into classes, hiding properties and methods implementation details, By limiting the access rights of class members such as public, protected, private, etc., the data is protected internally and can only be operated through authorized member methods, and members are encapsulated as much as possible.
public: Methods or properties can be accessed in any scope and are by default. If no access modifier is specified for a property or method, it will be public.
protected: This class and subclasses can be accessed, but external objects cannot be called.
private: can only be accessed in this class, and cannot be called by subclasses or external objects. Methods or attributes marked private can be redefined in inherited classes, and each class can only see its own defined private methods.
In terms of scope, these three modifiers should be sorted from large to small like this: public→protected→private. The reason why it is said to be in scope is because the class encapsulates some properties and methods. , this encapsulation determines the "visibility" of the data, so that we cannot modify the defined properties and methods at will outside the class but can only call them. This is the benefit of encapsulation, and it also improves security.
We give code examples:
class myClass{ public $public="Public"; //public属性 protected $protected="Protected"; //protected属性 private $private="Private"; //private 属性 function say_Hello() { //public属性 //只是举例说明,自行添加内容 } $obj=new myClass(); echo $obj->public; //echo $obj->protected; //echo $obj->private;
By running the above example we get a "Public", but when you remove the comment of //echo $obj->private; , you will get the following error:
Fatal error: Cannot access protected property myClass::$protected in E:apachehtdocsexamplefile.php on line 13.
You can see that we cannot access the attribute definitions of a class at will. We don't know "outside" what members are in this class, because these members may not be available to other classes. Of course, if we must access or modify attributes defined as "private", we can also use the system methods provided by PHP: _get() and _set().
Inheritance
You can make a class inherit and have the member properties and methods of another existing class. The inherited class is called the parent class or base class. Inheritance Class is a subclass. The inheritance relationship is implemented through the extends keyword. Generally speaking, to have inheritance, you must have a "root". For this "root", you may imagine that if you give birth to a son or daughter in the future, they will get some "things (properties and methods)" from you, so that your " Descendants" are those who possess some of the characteristics of you (the source).
Generate the "root" class (parent class or base class)
Syntax: class father{
}
Generate "descendants" ( Subclass)
Syntax: class son extends father{
}
PHP extends class inheritance example code:
class father{ protected $name; function construct($name){ //构造函数 $this->name=$name; } function work(){ echo "{$this->name}我在工作; } function destruct(){} //析构函数 } class son extends father{ //继承父类 function play(){ echo "{$this->name}我在玩游戏; } } $my_father=new father(“爸爸”); //创建父类对象 $my_father->work(); $my_son=new son(“儿子”); $my_son->work(); $my_son->play();
Analysis: In the parent class father, we define Common properties and methods, and then define subclasses. You may find that there are no constructors and destructors in the subclass, because the subclass inherits all the methods of the parent class, so you can call $my_son->work(); This is the PHP class inherit. Also note: PHP cannot have multi-level inheritance, such as: class A extends B extends C. Such inheritance is invalid in PHP. There is only single inheritance in PHP, not multiple inheritance. Other methods are needed to "implement" multiple inheritance in disguise.
The above is the detailed content of Detailed explanation of php class encapsulation and inheritance usage. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

类和方法的概念和实例类(Class):用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。方法:类中定义的函数。类的构造方法__init__():类有一个名为init()的特殊方法(构造方法),该方法在类实例化时会自动调用。实例变量:在类的声明中,属性是用变量来表示的,这种变量就称为实例变量,实例变量就是一个用self修饰的变量。实例化:创建一个类的实例,类的具体对象。继承:即一个派生类(derivedclass)继承基类(baseclass)的

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

jQuery是一种经典的JavaScript库,被广泛应用于网页开发中,它简化了在网页上处理事件、操作DOM元素和执行动画等操作。在使用jQuery时,经常会遇到需要替换元素的class名的情况,本文将介绍一些实用的方法,以及具体的代码示例。1.使用removeClass()和addClass()方法jQuery提供了removeClass()方法用于删除

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

class是python中的一个关键字,用来定义一个类,定义类的方法:class后面加一个空格然后加类名;类名规则:首字母大写,如果多个单词用驼峰命名法,如【class Dog()】。

去除方法:1、使用substr_replace()函数将首位数字替换为空字符串即可,语法“substr_replace($num,"",0,1)”;2、用substr截取从第二位数字开始的全部字符即可,语法“substr($num,1)”。

在编写PHP代码时,使用类(Class)是一个非常常见的做法。通过使用类,我们可以将相关的功能和数据封装在一个单独的单元中,使代码更加清晰、易读和易维护。本文将详细介绍PHPClass的用法,并提供具体的代码示例,帮助读者更好地理解如何在实际项目中应用类来优化代码。1.创建和使用类在PHP中,可以使用关键字class来定义一个类,并在类中定义属性和方法。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.