實(shí)例簡(jiǎn)介PHP的一些高級(jí)面向?qū)ο缶幊痰奶匦?/h1>
更新時(shí)間:2015年11月27日 15:51:58 投稿:goldensun
這篇文章主要以實(shí)例簡(jiǎn)單介紹了PHP的一些高級(jí)面向?qū)ο缶幊痰奶匦?包括在Java等OOP語言中所經(jīng)常見到的接口和抽象類等,需要的朋友可以參考下
一般來說,學(xué)習(xí)PHP需要了解下面的一些特性:
對(duì)象克隆。PHP5中對(duì)OOP模型的主要改進(jìn)之一,是將所有對(duì)象都看作引用,而不是值。但是,如果所有對(duì)象都視為引用,那么如何創(chuàng)建對(duì)象的副本呢?答案是通過克隆對(duì)象。
<?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());
?>
繼承。如前面所述,通過繼承來構(gòu)建類層次體系是OOP的關(guān)鍵概念。
class Employee {
...
}
class Executive extends Employee{
...
}
class CEO extends Executive{
...
}
接口。接口是一些未實(shí)現(xiàn)的方法定義和常量的集合,相當(dāng)于一種類藍(lán)本。接口只定義了類能做什么,而不涉及實(shí)現(xiàn)的細(xì)節(jié)。本章介紹PHP5對(duì)接口的支持,并提供了一些展示這個(gè)強(qiáng)大OOP特性的例子。
<?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);
?>
抽象類。抽象類實(shí)質(zhì)上就是無法實(shí)例化的類。抽象類將由可實(shí)例化的類繼承,后者稱為具體類(concreate class)。抽象類可以完全實(shí)現(xiàn)、部分實(shí)現(xiàn)或者根本未實(shí)現(xiàn)。
abstract class Class_name
{
//insert attribute definitions here
//insert method definitions here
}
命名空間。命名空間可根據(jù)上下文劃分各種庫(kù)和類,幫肋你更為有效地管理代碼庫(kù)。
<?php
namespace Library;
class Clean {
function printClean() {
echo "Clean...";
}
}
?>
<?php
include "test.php";
$clean = new \Library\Clean();
$clean->printClean();
?>
如果你使用過其他面向?qū)ο笳Z言,可能會(huì)感到奇怪,為什么上述特性沒有包括其他語言中熟悉的一些OOP特性?原因很簡(jiǎn)單,PHP不支持這些特性。為了讓你不再感到迷惑,下面列出PHP不支持的高級(jí)OOP特性。
- 方法重載。PHP不支持通過函數(shù)重載實(shí)現(xiàn)多態(tài),根據(jù)Zend網(wǎng)站的討論,可能永遠(yuǎn)都不會(huì)支持。要了解具體原因,可以查看http://www.zend.com/php/ask_experts.php
- 操作符重載。目前不支持根據(jù)所修改數(shù)據(jù)的類型為操作符賦予新的含義。根據(jù)zend網(wǎng)站的討論,將來實(shí)現(xiàn)這個(gè)特性的可能性也不大。
- 多重繼承。PHP不支持多重繼承。但是支持實(shí)現(xiàn)多個(gè)接口。
您可能感興趣的文章:- php學(xué)習(xí)筆記 php中面向?qū)ο笕筇匦灾籟封裝性]的應(yīng)用
- PHP5新特性: 更加面向?qū)ο蠡腜HP
- PHP 面向?qū)ο?final類與final方法
- PHP面向?qū)ο笾?深入理解static變量與方法
- php面向?qū)ο笾衧tatic靜態(tài)屬性和靜態(tài)方法的調(diào)用
- PHP面向?qū)ο笕筇攸c(diǎn)學(xué)習(xí)(充分理解抽象、封裝、繼承、多態(tài))
- php面向?qū)ο蟮姆椒ㄖ剌d兩種版本比較
- PHP面向?qū)ο蟮氖褂媒坛?簡(jiǎn)單數(shù)據(jù)庫(kù)連接
- PHP 面向?qū)ο笤斀?/a>
- PHP面向?qū)ο蟆L問修飾符介紹
- PHP入門教程之面向?qū)ο蟮奶匦苑治?繼承,多態(tài),接口,抽象類,抽象方法等)
相關(guān)文章
-
PHP 存儲(chǔ)文本換行實(shí)現(xiàn)方法
在文本存儲(chǔ)時(shí)使用\n如果發(fā)現(xiàn)沒有效果, 這時(shí)可以使用\r\n就可以了,希望對(duì)有需要的朋友有所幫助。 2010-01-01
-
隊(duì)列在編程中的實(shí)際應(yīng)用(php)
隊(duì)列(Queue)是運(yùn)算受到限制的一種線性表。只允許在表的一端進(jìn)行插入,而在另一端進(jìn)行刪除元素的線性表。隊(duì)尾(rear)是允許插入的一端。隊(duì)頭(front)是允許刪除的一端??贞?duì)列是不含元素的空表。 2010-09-09
-
PHP數(shù)據(jù)庫(kù)操作三:redis用法分析
這篇文章主要介紹了PHP數(shù)據(jù)庫(kù)操作redis用法,結(jié)合實(shí)例形式詳細(xì)分析了php安裝、使用redis的步驟、方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下 2017-08-08
-
php使用json-schema模塊實(shí)現(xiàn)json校驗(yàn)示例
這篇文章主要介紹了php使用json-schema模塊實(shí)現(xiàn)json校驗(yàn),結(jié)合實(shí)例形式分析了json-schema模塊的安裝及使用json-schema模塊進(jìn)行json校驗(yàn)的相關(guān)操作技巧,需要的朋友可以參考下 2019-09-09
-
php自定義函數(shù)實(shí)現(xiàn)二維數(shù)組按指定key排序的方法
這篇文章主要介紹了php自定義函數(shù)實(shí)現(xiàn)二維數(shù)組按指定key排序的方法,通過自定義函數(shù)實(shí)現(xiàn)二維數(shù)組按照指定鍵值進(jìn)行排序的功能,涉及數(shù)組的遍歷與判定相關(guān)操作技巧,需要的朋友可以參考下 2016-09-09
最新評(píng)論
一般來說,學(xué)習(xí)PHP需要了解下面的一些特性:
對(duì)象克隆。PHP5中對(duì)OOP模型的主要改進(jìn)之一,是將所有對(duì)象都看作引用,而不是值。但是,如果所有對(duì)象都視為引用,那么如何創(chuàng)建對(duì)象的副本呢?答案是通過克隆對(duì)象。
<?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());
?>
繼承。如前面所述,通過繼承來構(gòu)建類層次體系是OOP的關(guān)鍵概念。
class Employee {
...
}
class Executive extends Employee{
...
}
class CEO extends Executive{
...
}
接口。接口是一些未實(shí)現(xiàn)的方法定義和常量的集合,相當(dāng)于一種類藍(lán)本。接口只定義了類能做什么,而不涉及實(shí)現(xiàn)的細(xì)節(jié)。本章介紹PHP5對(duì)接口的支持,并提供了一些展示這個(gè)強(qiáng)大OOP特性的例子。
<?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);
?>
抽象類。抽象類實(shí)質(zhì)上就是無法實(shí)例化的類。抽象類將由可實(shí)例化的類繼承,后者稱為具體類(concreate class)。抽象類可以完全實(shí)現(xiàn)、部分實(shí)現(xiàn)或者根本未實(shí)現(xiàn)。
abstract class Class_name
{
//insert attribute definitions here
//insert method definitions here
}
命名空間。命名空間可根據(jù)上下文劃分各種庫(kù)和類,幫肋你更為有效地管理代碼庫(kù)。
<?php
namespace Library;
class Clean {
function printClean() {
echo "Clean...";
}
}
?>
<?php
include "test.php";
$clean = new \Library\Clean();
$clean->printClean();
?>
如果你使用過其他面向?qū)ο笳Z言,可能會(huì)感到奇怪,為什么上述特性沒有包括其他語言中熟悉的一些OOP特性?原因很簡(jiǎn)單,PHP不支持這些特性。為了讓你不再感到迷惑,下面列出PHP不支持的高級(jí)OOP特性。
- 方法重載。PHP不支持通過函數(shù)重載實(shí)現(xiàn)多態(tài),根據(jù)Zend網(wǎng)站的討論,可能永遠(yuǎn)都不會(huì)支持。要了解具體原因,可以查看http://www.zend.com/php/ask_experts.php
- 操作符重載。目前不支持根據(jù)所修改數(shù)據(jù)的類型為操作符賦予新的含義。根據(jù)zend網(wǎng)站的討論,將來實(shí)現(xiàn)這個(gè)特性的可能性也不大。
- 多重繼承。PHP不支持多重繼承。但是支持實(shí)現(xiàn)多個(gè)接口。
- php學(xué)習(xí)筆記 php中面向?qū)ο笕筇匦灾籟封裝性]的應(yīng)用
- PHP5新特性: 更加面向?qū)ο蠡腜HP
- PHP 面向?qū)ο?final類與final方法
- PHP面向?qū)ο笾?深入理解static變量與方法
- php面向?qū)ο笾衧tatic靜態(tài)屬性和靜態(tài)方法的調(diào)用
- PHP面向?qū)ο笕筇攸c(diǎn)學(xué)習(xí)(充分理解抽象、封裝、繼承、多態(tài))
- php面向?qū)ο蟮姆椒ㄖ剌d兩種版本比較
- PHP面向?qū)ο蟮氖褂媒坛?簡(jiǎn)單數(shù)據(jù)庫(kù)連接
- PHP 面向?qū)ο笤斀?/a>
- PHP面向?qū)ο蟆L問修飾符介紹
- PHP入門教程之面向?qū)ο蟮奶匦苑治?繼承,多態(tài),接口,抽象類,抽象方法等)
相關(guān)文章
PHP 存儲(chǔ)文本換行實(shí)現(xiàn)方法
在文本存儲(chǔ)時(shí)使用\n如果發(fā)現(xiàn)沒有效果, 這時(shí)可以使用\r\n就可以了,希望對(duì)有需要的朋友有所幫助。2010-01-01
隊(duì)列在編程中的實(shí)際應(yīng)用(php)
隊(duì)列(Queue)是運(yùn)算受到限制的一種線性表。只允許在表的一端進(jìn)行插入,而在另一端進(jìn)行刪除元素的線性表。隊(duì)尾(rear)是允許插入的一端。隊(duì)頭(front)是允許刪除的一端??贞?duì)列是不含元素的空表。2010-09-09
PHP數(shù)據(jù)庫(kù)操作三:redis用法分析
這篇文章主要介紹了PHP數(shù)據(jù)庫(kù)操作redis用法,結(jié)合實(shí)例形式詳細(xì)分析了php安裝、使用redis的步驟、方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
php使用json-schema模塊實(shí)現(xiàn)json校驗(yàn)示例
這篇文章主要介紹了php使用json-schema模塊實(shí)現(xiàn)json校驗(yàn),結(jié)合實(shí)例形式分析了json-schema模塊的安裝及使用json-schema模塊進(jìn)行json校驗(yàn)的相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
php自定義函數(shù)實(shí)現(xiàn)二維數(shù)組按指定key排序的方法
這篇文章主要介紹了php自定義函數(shù)實(shí)現(xiàn)二維數(shù)組按指定key排序的方法,通過自定義函數(shù)實(shí)現(xiàn)二維數(shù)組按照指定鍵值進(jìn)行排序的功能,涉及數(shù)組的遍歷與判定相關(guān)操作技巧,需要的朋友可以參考下2016-09-09

