search
HomeBackend DevelopmentPHP Tutorial实例简介PHP的一些高级面相对象编程的特性_PHP

一般来说,学习PHP需要了解下面的一些特性:

对象克隆。PHP5中对OOP模型的主要改进之一,是将所有对象都看作引用,而不是值。但是,如果所有对象都视为引用,那么如何创建对象的副本呢?答案是通过克隆对象。

<&#63;php
class Corporate_Drone{
 private $employeeid;
 private $tiecolor;
 function setEmployeeID($employeeid) {
 $this->employeeid = $employeeid;
 }

 function getEmployeeID() {
 return $this->employeeid;
 }
 
 function setTiecolor($tiecolor) {
 $this->tiecolor = $tiecolor;
 }
 
 function getTiecolor() {
 return $this->tiecolor;
 }
}

$drone1 = new Corporate_Drone();
$drone1->setEmployeeID("12345");
$drone1->setTiecolor("red");
$drone2 = clone $drone1;
$drone2->setEmployeeID("67890");

printf("drone1 employeeID:%d <br />",$drone1->getEmployeeID());
printf("drone1 tie color:%s <br />",$drone1->getTiecolor());
printf("drone2 employeeID:%d <br />",$drone2->getEmployeeID());
printf("drone2 tie color:%s <br />",$drone2->getTiecolor());
&#63;>

继承。如前面所述,通过继承来构建类层次体系是OOP的关键概念。

class Employee {
 ...
}

class Executive extends Employee{
 ...
}

class CEO extends Executive{
 ...
}

接口。接口是一些未实现的方法定义和常量的集合,相当于一种类蓝本。接口只定义了类能做什么,而不涉及实现的细节。本章介绍PHP5对接口的支持,并提供了一些展示这个强大OOP特性的例子。

<&#63;php
interface IPillage
{
 // CONST 1;
 function emptyBankAccount();
 function burnDocuments();
}

class Employee {

}

class Excutive extends Employee implements IPillage {
 private $totalStockOptions;
 function emptyBankAccount() {
 echo "Call CFO and ask to transfer funds to Swiss bank account";
 }
 function burnDocuments() {
 echo "Torch the office suite.";
 }
}

class test {
 function testIP(IPillage $ib) {
 echo $ib->emptyBankAccount();
 }
}
$excutive = new Excutive();
$test = new test();
echo $test->testIP($excutive);
&#63;>

抽象类。抽象类实质上就是无法实例化的类。抽象类将由可实例化的类继承,后者称为具体类(concreate class)。抽象类可以完全实现、部分实现或者根本未实现。

abstract class Class_name
{
 //insert attribute definitions here
 //insert method definitions here
}

命名空间。命名空间可根据上下文划分各种库和类,帮肋你更为有效地管理代码库。

<&#63;php
namespace Library;
class Clean {
 function printClean() {
 echo "Clean...";
 }
}
&#63;>

<&#63;php
include "test.php";
$clean = new \Library\Clean();
$clean->printClean();
&#63;>

如果你使用过其他面向对象语言,可能会感到奇怪,为什么上述特性没有包括其他语言中熟悉的一些OOP特性?原因很简单,PHP不支持这些特性。为了让你不再感到迷惑,下面列出PHP不支持的高级OOP特性。

  • 方法重载。PHP不支持通过函数重载实现多态,根据Zend网站的讨论,可能永远都不会支持。要了解具体原因,可以查看http://www.zend.com/php/ask_experts.php
  • 操作符重载。目前不支持根据所修改数据的类型为操作符赋予新的含义。根据zend网站的讨论,将来实现这个特性的可能性也不大。
  • 多重继承。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
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

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 Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor