PHP中类的继承和用法实例分析,php实例分析
本文实例讲述了PHP中类的继承和用法。分享给大家供大家参考,具体如下:
1、继承关键字 :extends
PHP类的继承,我们可以理解成共享被继承类的内容。PHP中使用extends单一继承的方法,请切忌!(非C++多继承)被继承的类我们叫做父类(基类)继承者成为子类(派生类)。
2、PHP继承的规则
CLASS1------>CLASS2------>CLASS3
依次被继承,class3拥有class1、class2所以功能和属性,避免方法和属性重名。
class Son{} 继承class root{};
class Son extends Root{};
3、基类方法重载和父类方法访问
因为属于向下继承的原理,基类不能使用派生类里内容,这个时基类的一些方法不能完成我们的一些派生类的功能,我们就可以进行方法重载避免了,新建方法带来的混乱。
方法重载我们也可以理解方法覆盖,在派生类里使用与基类方法重名的方法名称执行重载。
重载时我们需要调用原始基类内容,再增加新内容,我们可以使用
基类名 :: 方法名称。
实例:
<?php class Root{ function dayin(){ return "Root print <br />"; } } class Son extends Root{ function dayin(){ //return $this->dayin()."Son print <br/>"; return Root::dayin()."Son print <br />"; } } $s=new Son(); echo $s->dayin(); ?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software