php中使用接口實(shí)現(xiàn)工廠設(shè)計(jì)模式的代碼
Jun 13, 2016 pm 12:00 PM
接口在php只能起到約束類(lèi)的定義作用,雖不像c#/java那么直觀,但基于oop的封裝要求,使用接口可以提高程序的可擴(kuò)展性,如實(shí)現(xiàn)代理設(shè)計(jì)模式。
復(fù)制代碼 代碼如下:
//人類(lèi)接口
interface IHuman
{
function GetName();
}
//男人類(lèi),實(shí)現(xiàn)人類(lèi)接口
class ManClass implements IHuman
{
//獲取姓名方法
public function GetName()
{
return "I'm man."."
";
}
}
//女人類(lèi),實(shí)現(xiàn)人類(lèi)接口
class WomanClass implements IHuman
{
//獲取姓名方法
public function GetName()
{
return "I'm Woman."."
";
}
}
//類(lèi)工廠,根據(jù)需要生產(chǎn)不同實(shí)例對(duì)象返回
class ManFactory
{
//根據(jù)參數(shù)獲取實(shí)例對(duì)象
public function GetIHuman($IHuman="man")
{
if($IHuman=="woman")
{
return new WomanClass();
}
else if($IHuman=="man")
{
return new ManClass();
}
else
{
return null;
}
}
//直接獲取woman類(lèi)
public function GetWoman()
{
return new WomanClass();
//return new ManClass();
}
//直接獲取man類(lèi)
public function GetMan()
{
return new ManClass();
}
}
$ManFactory=new ManFactory();
$ManClass=$ManFactory->GetIHuman();
echo $ManClass->GetName();
$IHuman=$ManFactory->GetIHuman("woman");
echo $IHuman->GetName();
$Woman=$ManFactory->GetWoman();
echo $Woman->GetName();
$Man=$ManFactory->GetMan();
echo $Man->GetName();
?>
運(yùn)行結(jié)果:
I'm man.
I'm Woman.
I'm Woman.
I'm man.

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











phphasthreecommentstyles : //, #forsingle-lineand/.../formulti-lline.usecommentstoexplainwhycodeexists, notwhatitdoes.marktodo/fixMeitemsandDisableCodeTemporlinlyDuingDeBugging.aVoidOver-commentingsimplOgic.writeCoCoCoCoCoConcomeCOCOCOCONCOCOCOCOCOCOCOCOCISE

Windows? PHP? ???? ?? ???? ??? ?????. 1. ??? PHP ??? ?????? ?? ??????. Apache? ?? ThreadSafe ??? ????? Nginx??? ThreadSafe ??? ???? ?? ????. 2. php.ini ??? ???? php.ini-development ?? php.ini-production? php.ini? ?????. 3. ?? ? ??? ?? ??? ?? ?? ??? PHP ??? ??????. 4. PHP? ????? ?????? ??? ????? ?? ?? ?? PHP-V? ???? ?? ??? ???? ?? ?? ??? ??????. 5. Apache? ???? ?? httpd.conf?? p? ???????.

PHP? ?? ???? 4 ?? ?? ??? ?????. 1. PHP ??? ?????? ??? ??? ???? ?? ?????. 2. Echo ? Print? ????? ?? ???? ????, ??? Echo? ?? ?? ??? ?????? ??????. 3. ?? ???? ?? ???? ??????? //, # ? //; 4. ? ??? ?????? ????? ??? ?? ????? ??? ??? ??? ?? ???? ??? ????. ??? ?? ??? ????? ???? ???? PHP ??? ???? ? ??? ? ? ????.

?, ApythonclasscanhavemultiplecontructorsthrowaltiveTechniques.1.usedefaultargumentsinthe__init__methodtoallowflexibleinitializationswithvaryingnumbersofparameters.2.defineclassmethodsasaltistuctructorsforcecalobbebcreati

Python? Ifelse ??? ???? ??? ??? ??? ?? ??? ???? ????. 1. ???? ??? ???? ??? ???? ????. ??? ??? ?? ??? ???? ?? ?????. 2. ?? ?? ??? ELIF? ?? ????, ????? ???? ?? ???? ?????. 3. ?? ?? ??? ???? ?? ?? ? ?? ? ?? ???? ?? ?? ????. 4. 3 ?? ??? ??? ?????? ??? ifelse? ???? ? ??? ? ????. ?? ??, ??? ?? ? ??? ??????? ?????? ??? ???? ???? ?? ??? ??? ? ????.

???? PHP8? ???? ??? ??? ????. 1. ????? ??? ?? ????; 2. PHP8 ? ?? ?? ??? ??????. 3. ??? ????? ????? ??? ??????. 4. ??? ?? ?? ??? ??????. Windows ???? Zip ???? ?????? ?? ?? ? ?? ?? ??? ???? ???? ????? ?? ??? ??? ?? ? ? ????. MACOS ???? Homebrew? ???? ? ??, PHP8 ??, ?? ?? ?? ? ?? ??? ?? ??? ???? ?? ????. ?? ??? ?? ????? ???? ????? ????? ??? ?? ??? ??? ??? ? ????.

phpisaserver-sideScriptingLanguageUsedForWebDevelopment, ?? ProcessesData, InteractSwithDatabases ? SendShtmlTobrowsers.commonusesincludeusera-sectentication, e-commerceplatforms

? ?? PHP ???? ??? ???? ??? ?????? ?? ?? ?? ??? ???? XAMPP/MAMP/LAMP? ?? ? ?? ??? ???? ???? ??? ???? ??? ?????. ??, hello.php?? ??? ??? ?? ??? ???? ???? ??????. ??, PHP ? HTML? ???? ?? ??? ??? ???? ?? ????. ?????, ???? ??, ?? ?? ? ?? ?? ??? ?? ???? ?????? ???? ???? ?? ????? ???????.
