search
HomeBackend DevelopmentPHP TutorialPHP面向对象详解(三)_PHP

PHP面向对象详解(三)_PHP

May 28, 2016 pm 01:12 PM
php object-orientedobject-oriented

面向对象 对象概念是面向对象技术的核心。在显示世界里我们所面对的事情都是对象,如计算机、电视机、自行车等。在面向对象的程序设计中,对象是一个由信息及对信息进行处理的描述所组成的整体,是对现实世界的抽象.

对象的主要三个特性

对象的行为:可以对 对象施加那些操作,开灯,关灯就是行为。
对象的形态:当施加那些方法是对象如何响应,颜色,尺寸,外型。
对象的表示:对象的表示就相当于身份证,具体区分在相同的行为与状态下有什么不同。

面向对象模型

面向对象的概念:

oop(面向对象的编程)它能是其代码更加简洁易于维护并且具有更强的可重性

代码如下:


final public function test(){


依此类推,对于不想有子类的父类,在类名那里写上final

代码如下:


final class BaseClass{


代码如下:


//定义一个接口
interface ICanEat{
 public function eat($food);
}


可以看到,接口中并没有方法的具体实现,但必须有方法!

那么,下面是,“人类会吃”

//具体对象,连接到接口
class Human implements ICanEat{
public function eat($food){
 echo "Human eating ".$food.".<br/>";
 }
}
$obj=new Human();
$obj->eat("shit");

请忽略我给出的“食物”。

代码如下:


var_dump($obj instanceof ICanEat);


会返回boolean值。

--更多的栗子

interface ICanPee extends ICanEat{
 public function pee();
}
class Demon implements ICanPee{
 public function pee(){
 echo "Can demon pee&#63;";
 }
 public function eat($food){
 echo "Can demon eat ".$food;
 }
}
$ghost=new Demon();
$ghost->pee();
$ghost->eat("shit");

 接口本质上也是类,可以被继承/继承,但是使用继承接口的接口,所有父类、“爷类”的方法都要有具体实现。

--小结

/**
 * 接口
 * 1. 接口的基本概念和基本使用方法
 * 2. 接口里面的方法没有具体的实现
 * 3. 实现了某个接口的类必须提供接口中定义的方法
 * 4. 不能用接口创建对象,但是能够判断某个对象是否实现了某个接口
 * 5. 接口可以继承接口(interface extends interface)
 * 6. 接口中定义的所有方法都必须是公有,这是接口的特性。
 */

aaaaaaaaaaaaaa

bu xiang xie le....................

ming tian yao ge ..............

以上内容是小编给大家介绍的PHP面向对象详解(三),希望大家喜欢。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools