PHP ????
PHP ????
???? ?????(??: ???? ?????, ??: OOP)?? ??? ??? ? ??? ???? ??? ?? ???? ??? ?????. ?? ??? ???.
?? ???? ??? ???? ??? ???, ????, ??? ?? ?????.
??? ? ?? ?? ??:
· ??? ??: ??? ?? ??? ??? ? ???, ??? ?? ?? ?? ?????. .
· ??? ?? : ?? ??? ???? ? ??? ??? ?????, ??, ??, ??.
· ??? ??: ??? ??? ???? ????, ????? ??? ??? ??? ??? ????.
?? ?? Animal? ?? ?? ??? ? ?? ?? ?????, ?? ?? ?? ??? ???, ??? ? ??, ?? ?? ??? ????.
???? ???
· ??? - ??? ???? ??? ?????. ???? ???? ???? ??? ???? ?? ??? ?????.
· Object - ???? ???????.
· ?? ?? - ??? ??? ??? ??. ? ??? ?? ?? ???? ??? ??? ?? ??? ?? ???? ? ????. ???? ??? ??????? ??? ??? ????? ?? ? ????.
· ?? ?? - ??? ??? ???? ??? ???? ????? ? ??? ? ????.
· ?? - ??? ?? ???? ?? ??? ??? ??? ???? ???? ???? ???????. ?? ??? ?? ?????. ???? ???? ??? ? ?? ???? ???? ? ? ??, ?? ????? ??? ???? ??? ???? ???? ??? ???? ??? ? ????.
· ?? ??? - ???? ?? ???? ?? ?????. ? ???? ?? ???, ?? ??? ?? ?? ????? ? ? ????.
· ????? - ?? ???? ???? ???? ??????? ???? ??, ?? ?????? ???? ???.
·????????(Polymorphism) - ???? ??? ??, ?? ?? ????? ?? ??? ??? ???? ?? ?? ??? ?? ? ??? ?????. ??? ???? ??? ? ?? ?? ??? ?? ?? ??? ??? ? ?? ??? ?????? ???.
· ????(Overloading) - ??? ???, ??? ??? ???? ??? ?? ??? ???? ????? ?? ?? ????? ???.
·??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????. ???? ?? ?? ?? ???? ????? ??????? ??? ??? ??? ???? ??????. ?? ???? ??? ?????? ?? ?? ????? ????? ???.
· ???(Encapsulation) - ???? ?? ??? ???? ??? ??? ??? ??? ?? ??? ?? ?? ???? ?? ????.
· ??? - ??? ??? ? ??? ????? ? ?? ?????. ?, ?? ?? ??? ???? ???? ? ?????. ???? ?? new ???? ?? ?????. ??.
· ??? - ???(???) ????? ?? ??? ????? ???(?? ?? ??? ??? ??? ??? ??) ???? ???? ???? ?????. ???? ?? "??" ??? ???? ? ?????(?? ?? ??? ??? ? new? ???? ??? ??? ???. ??? ??? ???? ?? ????? ??? ?? ????? ???).
?? ????? Car ???? ?? Mercedes, Bmw ? Audi?? ? ?? ??? ??????.
????PHP ??? ??
<類> PHP ?? ??? ???? ?? ??? ??? ????.
$mercedes = new Car (); $bmw = new Car (); $audi = new Car ();??? ??? ????.
· ??? ???? ??? ???? ?? ??? ??. · ??? ?? ?? ???({}) ?? ??? ???? ??? ? ????. · ??? ??? var? ???? ????, ?? ???? ?????. · ?? ??? PHP ?? ??? ????? ???? ?????? ??? ???? ??? ???? ? ????. ????
<?php class phpClass { var $var1; var $var2 = "constant string"; function myfunc ($arg1, $arg2) { [..] } [..] } ?>$this ??? ?? ??? ?????. PHP_EOL? ?? ?????.
PHP?? ?? ??
?? ??? ??
??? ?????? ? ??? ???? ?? ???? ??? ? ????. object.member ??:
// ?? ?? ??, ?? ? URL ??
$runoob->setTitle( "PHP Chinese Network" );
$ taobao-> setTitle( "Taobao" );
$google->setTitle( "Google ??" );
$runoob->setUrl( ' www.ask.php.cn' );
$taobao->setUrl( 'www.taobao.com' );
$google->setUrl( 'www.google. com' );
// ??? URL? ???? ?? ?? ?? ??
$runoob->getTitle();
$ taobao->getTitle( );
$google->getTitle();
$runoob->getUrl();
$taobao->getUrl ();
$google->getUrl();
?? ??? ??? ????.
Instance
<?php class Site { /* 成員變量 */ var $url; var $title; /* 成員函數(shù) */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url . PHP_EOL; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . PHP_EOL; } } ?>
? ??? ???? ?? ??? ??? ????.
PHP ??? ????
Taobao
Google ??
www.ask.php.cn
www.taobao.com
www.google.com
PHP ???
???? ??? ??????. ?? ??? ??? ? ??? ????? ? ?????. ?, ?? ?? ??? ???? ???? ? ?????. ??? ???? ???? ?? new ???? ?? ?????.
PHP 5??? ???? ???? ???? ???? ??? ? ????. ?? ??? ??? ????.
void __construct ([ Mixed $args [, $... ]] )
? ???? ??? ???? ?? $url ? $title ??? ???? ? ????.
function __construct( $par1, $par2 ) {
$this - >url = $par1;
$this->title = $par2;
}
?? ? ?? setTitle ? setUrl ???? ??? ??? ????.
????
<?php class Site { /* 成員變量 */ var $url; var $title; /* 成員函數(shù) */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url . PHP_EOL; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . PHP_EOL; } } $runoob = new Site; $taobao = new Site; $google = new Site; // 調(diào)用成員函數(shù),設(shè)置標(biāo)題和URL $runoob->setTitle( " PHP中文網(wǎng)" ); $taobao->setTitle( "淘寶" ); $google->setTitle( "Google 搜索" ); $runoob->setUrl( 'www. ask.php.cn ' ); $taobao->setUrl( 'www.taobao.com' ); $google->setUrl( 'www.google.com' ); // 調(diào)用成員函數(shù),獲取標(biāo)題和URL $runoob->getTitle(); $taobao->getTitle(); $google->getTitle(); $runoob->getUrl(); $taobao->getUrl(); $google->getUrl(); ?>
???
???(???) ????? ??? ??? ????? ????(?? ?? ??? ??? ??? ??? ??) ???? ???? ???? ?????.
PHP 5??? ?? ?? ?? ??? ??? ??? ??? ??????. ?? ??? ??? ????.
void __destruct ( void )
Instance
$runoob = new Site('www.runoob.com', 'PHP中文網(wǎng)'); $taobao = new Site('www.taobao.com', '淘寶'); $google = new Site('www.google.com', 'Google 搜索'); // 調(diào)用成員函數(shù),獲取標(biāo)題和URL $runoob->getTitle(); $taobao->getTitle(); $google->getTitle(); $runoob->getUrl(); $taobao->getUrl(); $google->getUrl();
? ? ??? ???? ?? ??? ??? ????.
Constructor
Destroy MyDestructableClass
??
PHP? ???? ???? ?? ?? ???? ?????. PHP? ?? ??? ???? ????.
class Child. extends Parent {
// ?? ??
}
Instance
????? Child_Site ???? Site ???? ???? ??? ?????.
<?php
// ?? ???? ??? ????? ?????
class Child_Site? ??? {
var $category;
function setCate($par){
$this->category = $par;
}
function getCate() {
}
}
>
??? ???
?? ????? ???? ???? ?? ???? ?? ??? ??? ? ?? ?? ??? ?? , ? ????? ??? ????? ?? ??? ??????? ???.
return $this->title;}
??? ??
PHP? ???? ???? ?? ?? ??? ?? public(public), protected(protected) ?? private(private) ???? ???? ??????.
?? ??? ??
??? ??? ??, ??, ??? ? ??? ????? ???. var? ???? ??? ?????.
<?php class MyDestructableClass { function __construct() { print "構(gòu)造函數(shù)\n"; $this->name = "MyDestructableClass"; } function __destruct() { print "銷毀 " . $this->name . "\n"; } } $obj = new MyDestructableClass(); ?>???? ?? ??? ??
???? ???? ??, ??? ?? ??? ??? ? ????. ??? ???? ???? ?? ?? ???? ????? public?? ?????.
<?php /** * Define MyClass */ class MyClass { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj = new MyClass(); echo $obj->public; // 這行能被正常執(zhí)行 echo $obj->protected; // 這行會產(chǎn)生一個致命錯誤 echo $obj->private; // 這行也會產(chǎn)生一個致命錯誤 $obj->printHello(); // 輸出 Public、Protected 和 Private /** * Define MyClass2 */ class MyClass2 extends MyClass { // 可以對 public 和 protected 進(jìn)行重定義,但 private 而不能 protected $protected = 'Protected2'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj2 = new MyClass2(); echo $obj2->public; // 這行能被正常執(zhí)行 echo $obj2->private; // 未定義 private echo $obj2->protected; // 這行會產(chǎn)生一個致命錯誤 $obj2->printHello(); // 輸出 Public、Protected2 和 Undefined ?>?????
?????? ???? ???? ???? ?? ???? ??? ? ??? ???? ??? ??? ? ????. ??? ?? ? ??? ???? ???.
?????? ?? ??? ??? ????? ????? ???? ?? ?????, ? ?? ??? ???? ?? ?? ????. ?????? ??? ?? ???? ????? ???. ?? ?????? ?????. ?????? ????? Implements ???? ?????. ???? ?????? ??? ?? ???? ???? ???. ??? ??? ???? ??? ?????. ???? ?? ?????? ??? ? ????. ??? ???? ?? ?????? ??? ?????.????
??
??? ??? ?? ??? ?? ?? ??? ??? ? ????. ??? ???? ??? ? $ ??? ??? ??? ????.
??? ?? ??? ???? ?? ??, ??? ??, ?? ??? ?? ?? ?? ??? ? ? ????.
PHP 5.3.0?? ??? ???? ???? ???? ??? ? ????. ??? ? ??? ?? ???(?: self, parent ?? static)? ? ????.
????
<?php /** * Define MyClass */ class MyClass { // 聲明一個公有的構(gòu)造函數(shù) public function __construct() { } // 聲明一個公有的方法 public function MyPublic() { } // 聲明一個受保護(hù)的方法 protected function MyProtected() { } // 聲明一個私有的方法 private function MyPrivate() { } // 此方法為公有 function Foo() { $this->MyPublic(); $this->MyProtected(); $this->MyPrivate(); } } $myclass = new MyClass; $myclass->MyPublic(); // 這行能被正常執(zhí)行 $myclass->MyProtected(); // 這行會產(chǎn)生一個致命錯誤 $myclass->MyPrivate(); // 這行會產(chǎn)生一個致命錯誤 $myclass->Foo(); // 公有,受保護(hù),私有都可以執(zhí)行 /** * Define MyClass2 */ class MyClass2 extends MyClass { // 此方法為公有 function Foo2() { $this->MyPublic(); $this->MyProtected(); $this->MyPrivate(); // 這行會產(chǎn)生一個致命錯誤 } } $myclass2 = new MyClass2; $myclass2->MyPublic(); // 這行能被正常執(zhí)行 $myclass2->Foo2(); // 公有的和受保護(hù)的都可執(zhí)行,但私有的不行 class Bar { public function test() { $this->testPrivate(); $this->testPublic(); } public function testPublic() { echo "Bar::testPublic\n"; } private function testPrivate() { echo "Bar::testPrivate\n"; } } class Foo extends Bar { public function testPublic() { echo "Foo::testPublic\n"; } private function testPrivate() { echo "Foo::testPrivate\n"; } } $myFoo = new foo(); $myFoo->test(); // Bar::testPrivate // Foo::testPublic ?>
?? ???
???? ?? ?? ?? ?? ?? ??? ???? ???? ???? ???? ???? ????? ???.
???? ??? ???? ?????? ? ????.
???? ??? ???? ?? ???(????)? ???? ?? ?? ??? ??? ?? ????.
?? ???? ??? ? ?? ???? ?? ???? ?? ?? ???? ???? ??, ??? ???? ??? ??? ?? ???? ????? ? ????? ???. ?? ?? ?? ???? protected? ??? ?? ?? ???? ??? ???? protected ?? public?? ????? ?? private?? ??? ? ????. ?? ???? ???? ???? ???? ???. ?, ?? ????? ??? ??? ????? ???. ?? ??, ?? ???? ?? ???? ?? ??? ??? ???? ?? ??? ?? ??? ???? ?? ? ?? ?? ??? ????.
<?php // 聲明一個'iTemplate'接口 interface iTemplate { public function setVariable($name, $var); public function getHtml($template); } // 實現(xiàn)接口 class Template implements iTemplate { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } public function getHtml($template) { foreach($this->vars as $name => $value) { $template = str_replace('{' . $name . '}', $value, $template); } return $template; } } ?>
? ? ??? ???? ?? ??? ??? ????.
ConcreteClass1
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2
?? ???
??? ???? ???? ??(static)?? ???? ???? ??????? ??? ?? ??? ? ????.
?? ??? ?????? ???? ??? ?? ???? ? ????(??? ?? ???? ???).
?? ?????? ?? ??? ???? ???? ?? ?????? ?? ?? $this? ??? ? ????.
??? -> ???? ?? ?? ??? ???? ? ????.
PHP 5.3.0?? ??? ???? ???? ???? ??? ? ????. ??? ? ??? ?? ??? self, parent ?? static? ? ????.
<?php class MyClass { const constant = '常量值'; function showConstant() { echo self::constant . PHP_EOL; } } echo MyClass::constant . PHP_EOL; $classname = "MyClass"; echo $classname::constant . PHP_EOL; // 自 5.3.0 起 $class = new MyClass(); $class->showConstant(); echo $class::constant . PHP_EOL; // 自 PHP 5.3.0 起 ?>
?
? ????? ???? ?? ??? ??? ????.
foo
foo
?? ???
PHP 5??? ??? ?? ???? ???????. ?? ???? ???? final? ???? ?? ???? ?? ???? ???? ? ????. ???? final? ???? ??? ? ????.
?? ??? ?? ? ??? ?????.
<?php abstract class AbstractClass { // 強(qiáng)制要求子類定義這些方法 abstract protected function getValue(); abstract protected function prefixValue($prefix); // 普通方法(非抽象方法) public function printOut() { print $this->getValue() . PHP_EOL; } } class ConcreteClass1 extends AbstractClass { protected function getValue() { return "ConcreteClass1"; } public function prefixValue($prefix) { return "{$prefix}ConcreteClass1"; } } class ConcreteClass2 extends AbstractClass { public function getValue() { return "ConcreteClass2"; } public function prefixValue($prefix) { return "{$prefix}ConcreteClass2"; } } $class1 = new ConcreteClass1; $class1->printOut(); echo $class1->prefixValue('FOO_') . PHP_EOL; $class2 = new ConcreteClass2; $class2->printOut(); echo $class2->prefixValue('FOO_') . PHP_EOL; ?>
?? ??? ??? ??
PHP? ??? ????. ?? ???? ???? ?? ???? ????? ???? ?????. ?? ???? ???? ????? ?? ???? ????? parent::__construct()? ???? ???.
<?php class Foo { public static $my_static = 'foo'; public function staticValue() { return self::$my_static; } } print Foo::$my_static . PHP_EOL; $foo = new Foo(); print $foo->staticValue() . PHP_EOL; ?>
? ????? ???? ?? ??? ??? ????.
BaseClass ???? ?? ??
BaseClass ???? ?? ??
SubClass ???? ?? ???
BaseClass ???? ?? ???