PHP ????
?? ?? ?????(??: ?? ?? ?????, ??: OOP)?? ??? ??? ??? ???? ??? ?? ???? ??? ?????.
?? ???? ??? ???? ??? ???, ????, ??? ?? ?????.
??? ? ?? ?? ??:
· ??? ??: ??? ?? ??? ??? ? ??? , ??? ??, ??? ????. ?? ?????.
· ??? ?? : ?? ??? ???? ? ??? ??? ?????, ??, ??, ??.
· ??? ??: ??? ??? ???? ????, ????? ??? ??? ??? ??? ????.
?? ?? Animal? ?? ?? ??? ? ?? ?? ?????, ?? ?? ?? ??? ???, ??? ? ??, ?? ?? ??? ????.
?? ?? ???
· ??? - ??? ???? ??? ?????. ???? ???? ???? ??? ???? ?? ??? ?????.
· Object - ???? ???????.
· ?? ?? - ??? ??? ??? ??. ? ??? ?? ?? ???? ??? ??? ?? ??? ?? ???? ? ????. ???? ??? ??????? ??? ??? ????? ?? ? ????.
· ?? ?? - ??? ??? ???? ??? ???? ???? ? ??? ? ????.
· ?? - ??? ?? ???? ?? ???? ??? ??? ???? ???? ???? ???????. ?? ??? ?? ?????. ???? ???? ??? ? ?? ???? ???? ? ? ??, ?? ????? ??? ???? ??? ???? ???? ??? ???? ??? ? ????.
· ?? ??? - ???? ?? ???? ?? ?????. ? ???? ?? ???, ?? ??? ?? ?? ????? ? ? ????.
· ????? - ?? ???? ???? ???? ??????? ???? ??, ?? ?????? ???? ???.
· ??? - ???? ??? ??, ?? ?? ????? ?? ??? ??? ???? ?? ?? ??? ?? ? ??? ?????. ??? ???? ??? ? ?? ?? ??? ?? ?? ??? ??? ? ?? ??? ?????? ???.
· ????(Overloading) - ??? ???, ??? ??? ???? ??? ?? ??? ???? ????? ?? ?? ????? ???.
· ??? - ???? ??? ??? ??(??)? ??(??)? ?? ??? ???? ????? ?? ????. ???? ?? ?? ?? ???? ????? ??????? ??? ??? ??? ???? ??????. ?? ???? ??? ?????? ?? ?? ????? ????? ???.
· ???(Encapsulation) - ???? ?? ??? ???? ??? ??? ??? ??? ??? ??? ?? ?? ???.
· ??? - ??? ??? ? ??? ????? ? ?? ?????. ?, ?? ?? ??? ???? ???? ? ?????. ??? ???? ???? ?? new ???? ?? ?????. .
· ??? - ???(???) ????? ?? ??? ????? ???(?? ?? ??? ??? ??? ??? ??) ???? ???? ???? ?????. ???? ?? "??" ??? ???? ? ?????(?? ?? ??? ??? ? new? ???? ??? ??? ???. ??? ??? ???? ?? ????? ??? ?? ????? ???).
?? ????? Car ???? ?? Mercedes, Bmw ? Audi?? ? ?? ??? ??????.
$mercedes = ? ???();
$bmw = ? ???();
$audi = ? ???() ;
PHP ??? ??
PHP ???? ???? ???? ?? ??? ??? ????.
<?php class phpClass { var $var1; var $var2 = "constant string"; function myfunc ($arg1, $arg2) { [..] } [..] } ?>
? ??? ?? ?? ?????.
· ??? ??? class ??? ?? ??? ?? ??? ?????.
· ??? ?? ?? ???({}) ?? ??? ???? ??? ? ????.
· ??? ??? var? ???? ????, ?? ???? ?????.
· ?? ??? PHP ?? ??? ????? ??? ???? ?????? ??? ???? ???? ? ????.
Instance
<?php class Site { /* 成員變量 */ var $url; var $title; /* 成員函數 */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url ."<br/>"; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . "<br/>"; } }?>
$this ??? ??? ??? ?????.
PHP?? ?? ??
???? ??? ? new ???? ???? ? ???? ??? ?????? ? ????.
$php = ? ???;
$taobao = ? ???;
$google = ? ???;
? ????? ? ?? ??? ??????. ? ?? ??? ?? ??????. ???? ?? ???? ?? ??? ????? ??? ???????.
// ?? ?? ??, ?? ? URL ??
$php->setTitle( "php中文網" );
$taobao-> setTitle( "????" );
$google->setTitle( "?? ??" );
$php->setUrl( 'miracleart.cn' );
$taobao ->setUrl( 'www.taobao.com' );
$google->setUrl( 'www.google.com' );
// ??? ???? ?? ?? ??? ???? URL
$php->getTitle();
$taobao->getTitle();
$google->getTitle();
$php->getUrl( );
$taobao->getUrl();
$google->getUrl();
??? ?? ??? ?????.
?? ??? ??? ????.
<?php header("Content-type:text/html;charset=utf-8"); //設置編碼 class Site { /* 成員變量 */ var $url; var $title; /* 成員函數 */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url ."<br/>"; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . "<br/>"; } } $php = new Site; $taobao = new Site; $google = new Site; // 調用成員函數,設置標題和URL $php->setTitle( "php中文網" ); $taobao->setTitle( "淘寶" ); $google->setTitle( "Google 搜索" ); $php->setUrl( 'miracleart.cn' ); $taobao->setUrl( 'www.taobao.com' ); $google->setUrl( 'www.google.com' ); // 調用成員函數,獲取標題和URL $php->getTitle(); $taobao->getTitle(); $google->getTitle(); $php->getUrl(); $taobao->getUrl(); $google->getUrl(); ?>
???? ?? ??:
php ??? ????
Taobao
Google ??
miracleart.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 header("Content-type:text/html;charset=utf-8"); //設置編碼 class Site { /* 成員變量 */ var $url; var $title; /* 成員函數 */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url ."<br/>"; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . "<br/>"; } function __construct( $par1, $par2 ) { $this->url = $par1; $this->title = $par2; } } // 調用成員函數,設置標題和URL $php = new Site('miracleart.cn', 'php中文網'); $taobao = new Site('www.taobao.com', '淘寶'); $google = new Site('www.google.com', 'Google 搜索'); // 調用成員函數,獲取標題和URL $php->getTitle(); $taobao->getTitle(); $google->getTitle(); $php->getUrl(); $taobao->getUrl(); $google->getUrl(); ?>
???? ?? ??:
php ??? ????
Taobao
Google ??
miracleart.cn
www.taobao.com
www.google.com
?? ???
???(destructor) ???? ??? ??? ?? ??? ???? ??(?: ??? ??? ??) ???? ???? ???? ?????.
PHP 5??? ?? ?? ?? ??? ??? ??? ??? ??????. ?? ??? ??? ????.
void __destruct (void )
Instance
<?php header("Content-type:text/html;charset=utf-8"); //設置編碼 class MyDestructableClass { function __construct() { print "構造函數\n"; $this->name = "MyDestructableClass"; } function __destruct() { print "銷毀 " . $this->name . "\n"; } } $obj = new MyDestructableClass(); ?>
???? ?? ??:
??? Destroy MyDestructableClass
Inherit
PHP? ??? ??? ???? ???? ????, PHP? ?? ??? ???? ????. ??? ??? ????.
class Child Extensions Parent {
// ?? ??
}
? ?? Child ???? Parent ???
Instance
? ????? ?????. ?? ??? Child_Site ???? Site ???? ???? ?????. ??:
<?php // 子類擴展站點類別 class Child_Site extends Site { var $category; function setCate($par){ $this->category = $par; } function getCate(){ echo $this->category . "<br/>"; } } ?>
??? ???
?? ????? ??? ???? ?? ???? ?? ??? ??? ? ?? ?? ?? ??? ? ????. ? ????? ??? ????? ?? ??? ?? ????? ???.
?? ???? getUrl ? getTitle ???? ?? ?????.
function getUrl() {
echo $this->url . PHP_EOL;
return $this->url;
}
function getTitle(){
echo $this->title . PHP_EOL;
return $this->title;
}
??? ??
PHP? ???? ???? ?? ??? ??? ?? public, protected ?? private ???? ???? ?????.
· ??(public) : ?? ??? ??? ???? ???? ? ????.
· protected : Protected ??? ??? ?? ??? ??? ?? ?? ??? ? ?? ????? ???? ? ????.
· private : Private ??? ??? ??? ??? ?????? ??? ? ????.
?? ??? ??
??? ??? ??, ??, ??? ? ??? ????? ???. var? ???? ??? ?????.
Instance
<?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; // 這行會產生一個致命錯誤 echo $obj->private; // 這行也會產生一個致命錯誤 $obj->printHello(); // 輸出 Public、Protected 和 Private /** * Define MyClass2 */ class MyClass2 extends MyClass { // 可以對 public 和 protected 進行重定義,但 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; // 這行會產生一個致命錯誤 $obj2->printHello(); // 輸出 Public、Protected2 和 Undefined ?>
? ??? ? ?????
??? ?? ??
???? ???? ??, ??? ?? ??? ??? ? ????. ??? ???? ???? ?? ?? ???? ????? public?? ?????.
????
rreee?????
????? ?? (?????)??? ?? ???? ???? ?? ???? ??? ? ??? ??? ???? ?? ??? ??? ??? ????.
?????? ?? ??? ??? ????? ????? ???? ?? ?????, ? ?? ??? ???? ?? ?? ????.
?????? ??? ?? ???? ????? ???. ?? ?????? ?????.
?????? ????? Implements ???? ?????. ???? ?????? ??? ?? ???? ???? ???. ??? ??? ???? ??? ?????. ???? ?? ?????? ??? ? ????. ??? ???? ?? ?????? ??? ?????.
????
<?php /** * Define MyClass */ class MyClass { // 聲明一個公有的構造函數 public function __construct() { } // 聲明一個公有的方法 public function MyPublic() { } // 聲明一個受保護的方法 protected function MyProtected() { } // 聲明一個私有的方法 private function MyPrivate() { } // 此方法為公有 function Foo() { $this->MyPublic(); $this->MyProtected(); $this->MyPrivate(); } } $myclass = new MyClass; $myclass->MyPublic(); // 這行能被正常執(zhí)行 $myclass->MyProtected(); // 這行會產生一個致命錯誤 $myclass->MyPrivate(); // 這行會產生一個致命錯誤 $myclass->Foo(); // 公有,受保護,私有都可以執(zhí)行 /** * Define MyClass2 */ class MyClass2 extends MyClass { // 此方法為公有 function Foo2() { $this->MyPublic(); $this->MyProtected(); $this->MyPrivate(); // 這行會產生一個致命錯誤 } } $myclass2 = new MyClass2; $myclass2->MyPublic(); // 這行能被正常執(zhí)行 $myclass2->Foo2(); // 公有的和受保護的都可執(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 ?>
??
??? ??? ??? ?? ?? ??? ??? ? ????. ??? ???? ??? ? $ ??? ??? ??? ????.
??? ?? ??? ???? ?? ??, ??? ??, ?? ??? ?? ?? ?? ??? ? ? ????.
PHP 5.3.0?? ??? ???? ???? ???? ??? ? ????. ??? ? ??? ?? ???(?: self, parent ?? static)? ? ????.
????
???????
?? ??? ??? ???? ???? ??? ?? ???? ???? ????? ???.
???? ??? ???? ?????? ? ????.
???? ??? ???? ?? ???(????)? ???? ?? ?? ??? ??? ?? ????.
?? ???? ??? ? ?? ???? ?? ???? ?? ?? ???? ???? ??, ??? ???? ??? ??? ?? ???? ??? ??? ???? ???. ). ?? ?? ?? ???? protected? ??? ??
?? ???? ??? ???? protected ?? public?? ????? ?? private?? ??? ? ????. ?? ???? ???? ???? ???? ???. ?, ?? ????? ??? ??? ????? ???. ?? ?? ?? ???? ??? ?? ??
? ????? ?? ???? ?? ??? ??? ???? ?? ?? ? ?? ?? ??? ????.
Instance
<?php // 聲明一個'iTemplate'接口 interface iTemplate { public function setVariable($name, $var); public function getHtml($template); } // 實現接口 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 . "<br/>"; } } echo MyClass::constant . "<br/>"; $classname = "MyClass"; echo $classname::constant . "<br/>"; // 自 5.3.0 起 $class = new MyClass(); $class->showConstant(); echo $class::constant . "<br/>"; // 自 PHP 5.3.0 起 ?>
???? ?? ??:
foo
foo
?? ???
PHP 5?? ??? ?? ???? ???????. ?? ???? ???? final? ???? ?? ???? ?? ???? ???? ? ????. ???? final? ???? ??? ? ????.
????
?? ?? ??? ??? ?????:
<?php 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_') . "<br/>"; $class2 = new ConcreteClass2; $class2->printOut(); echo $class2->prefixValue('FOO_') . "<br/>"; ?>
???? ?? ??:
??? ??: 16?? D:WWWBasisoopopp_9.php?? ?? ??? BaseClass::moreTesting()? ???? ? ????.
?? ??? ??? ??? ??
PHP? ?? ???? ????? ?? ???? ???? ???? ???? ????. ?? ???? ???? ????? ????? parent::__construct()
Instance
<?php class Foo { public static $my_static = 'foo'; public function staticValue() { return self::$my_static; } } print Foo::$my_static . "<br/>"; $foo = new Foo(); print $foo->staticValue() . "<br/>"; ?>
???? ???. ?? ??? ??>???? ?? ??:
BaseClass ???? ??? ???BaseClass ???? ??? ???
SubClass ???? ??? ???
BaseClass ???? ??? ???