php实现多重继承实例
最近一做php开发的朋友问了一个关于php多重继承的问题,两个人说了半天,其实自己也没有搞懂什么是多重继承,今天有空,特意将多重继承这个概念给详细的了解了一下,也来谈谈在php中实现多重继承的一些看法。
说多重继承之前首先说下与其相对的单一继承,单一继承指的是一个类只可以继承自一个父类,从现实生活中举例就是说一个儿子只有一个父亲。那么多重继承就好理解了,多重继承指的是一个类可以同时从多于一个父类继承行为与特征的功能。这个有逆常理,即一个儿子可以有多个父亲。由于多重继承是面向对象编程语言中所特有的特性,所以在php5之前是没有什么继承这一说了。但在php中,即使php5也只是实现了单继承。但是我们可以通过其它特殊的方式实现类的多重继承,比如使用接口(interface),只要把类的特征抽象为接口,并通过实现接口的方式让对象有多重身份,通过这样就可以在php中模拟多重继承了。
下面通过一个实例来说明一下如何在php中实现多重继承,具体代码如下:
<?php interface UserInterface{ //定义User的接口 function getname(); } interface TeacherInterface{ //teacher相关接口 function getLengthOfService(); } class User implements UserInterface{ //实现UserInterface接口 private $; public function getName(){ return $this->name; } } class Teacher implements TeacherInterface{ //实现TeacherInterface接口 private $lengthOfService=5; // 工龄 public function getLengthOfService(){ return $this->lengthOfService; } } // 继承自User类,同时实现了TeacherInterface接口. class GraduateStudent extends User implements TeacherInterface{ private $teacher ; public function __construct(){ $this->teacher=new Teacher(); } public function getLengthOfService(){ return $this->teacher->getLengthOfService(); } } class Act{ //注意这里的类型提示改成了接口类型 public static function getUserName(UserInterface $_user){ echo "Name is " . $_user->getName() ."<br>"; } //这里的类型提示改成了TeacherInterface类型. public static function getLengthOfService(TeacherInterface $_teacher){ echo "Age is " .$_teacher->getLengthOfService() ."<br>"; } } $graduateStudent=new GraduateStudent(); Act::getUserName($graduateStudent); Act::getLengthOfService($graduateStudent); //结果正如我们所要的,实现了有多重身份的一个对象
示例运行结果如下:
Name is tom
Age is 5
另外不得不说明的一下是多重继承在实际开发中会增加程式的复杂性与含糊性,非常不利于代码的调试。所以在开发中能够想办法用单继承的来实现的东西最好避免使用多重继承。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

Usage and Function of Vue.use Function Vue is a popular front-end framework that provides many useful features and functions. One of them is the Vue.use function, which allows us to use plugins in Vue applications. This article will introduce the usage and function of the Vue.use function and provide some code examples. The basic usage of the Vue.use function is very simple, just call it before Vue is instantiated, passing in the plugin you want to use as a parameter. Here is a simple example: //Introduce and use the plug-in

The file_exists method checks whether a file or directory exists. It accepts as argument the path of the file or directory to be checked. Here's what it's used for - it's useful when you need to know if a file exists before processing it. This way, when creating a new file, you can use this function to know if the file already exists. Syntax file_exists($file_path) Parameters file_path - Set the path of the file or directory to be checked for existence. Required. Return file_exists() method returns. Returns TrueFalse if the file or directory exists, if the file or directory does not exist Example let us see a check for "candidate.txt" file and even if the file

The usage of js function function is: 1. Declare function; 2. Call function; 3. Function parameters; 4. Function return value; 5. Anonymous function; 6. Function as parameter; 7. Function scope; 8. Recursive function.

The clearstatcache() function is used to clear the file status cache. PHP caches the information returned by the following functions −stat()lstat()file_exists()is_writable()is_readable()is_executable()is_file()is_dir()filegroup()fileowner()filesize()filetype()fileperms() What to do To provide better performance. Syntax voidclearstatecache() Parameter NA Return value clearstatcache(

With the development of the Internet, SOA (service-oriented architecture) has become an important technical architecture in today's enterprise-level systems. Services in the SOA architecture can be reused, reorganized and extended, while also simplifying the system development and maintenance process. As a widely used Web programming language, PHP also provides some function libraries for implementing SOA. Next, we will detail how to use SOA functions in PHP. 1. The basic concept of SOA. SOA is a distributed system development idea and architecture.
