???? ?? PHP ???? ??
?? ????
??? ??? ?? ?? ??? ?? ???? ??? ?????. ?? ?? ??? ???? ????.
?? ?? ????????. , OOP??? ???
?? ???? ??? ???? ?? ???, ?, ? ?? ?????.
??? 3? ??
1. ?? ??: ??? ??? ? ?? ??, ? ??? ?? ?? ??? ?????.
2. ??? ??: ?? ??? ???? ? ??? ???? ??, ??, ??, ??.
3. ??? ??: ??? ??? ???? ????, ?? ??? ??? ??? ??? ?????.
???? ???
1.???: ??? ???? ??? ?????. ???? ???? ???? ??? ???? ?? ??? ?????
2.??: ???? ???????
3.?? ??: ??? ??? ??? ?????. ? ??? ?? ???? ??? ??? ?? ??? ?? ??? ? ????. ???? ??? ??????? ??? ??? ????? ?? ? ????
4. ?? ??: ??? ??? ???? ??? ???? ????? ? ??? ? ????
5.??: ??? ?? ???? ???? ??? ??? ???? ???????. ? ?? ???? ??? ??? ?????. ???? ???? ??? ? ?? ???? ???? ? ?? ??, ?? ????? ??? ???? ??? ???? ????, ??? ???? ??? ?? ????
6 .?? ???: ???? ?? ???? ?? ?????. ? ???? ?? ???, ?? ??? ?? ?? ???
7. ?? ??? ?? ?? ? ????. ?? ???? ???? ???? ?? ????? ??, ?? ?????? ?? ? ????
8. ???: ???? ??? ???? ??? ????, ????? ?? ???? ??? ? ????. ??? ???? ?? ??? ????. ??? ???? ?? ??? ?? ?? ??? ?? ??? ??? ? ?? ??? ???
9.????: ?? ??? ??? ???? ??? ??? ????? ????? ?????. ??? ??? ??? ??? ?? ????? ?? ??? ???? ????? ??? ????? ???
10.???: ???? ??? ??? ??(??)? ??? ????? ?? ????. ) ? ??(??)? ???? ?????. ???? ?? ?? ?? ???? ????? ??????? ??? ??? ??? ???? ??????. ???? ??? ?????? ?? ??????? ??? ??? ???
11.???: ???? ?? ??? ???? ??? ??? ??? ??? ?? ?? ?????. ?? ??
12.???? ????, ?? ?? ? ??? ????? ? ?? ?????. ?, ?? ?? ??? ?? ?? ?????. ??? ???? ?????
? ?? ???? ??? ??13.??? : ???(destructor) ????? ?? ??? ????? ???(?? ?? ??? ??? ??? ??? ??) ???? ???? ???? ???. ???? ?? "??" ??? ???? ? ?????(?? ?? ??? ??? ? new? ???? ??? ??? ???. ??? ??? ???? ?? ????? ??? ?? ????? ???)
??? ??:
1. ???? ??? ??? ?? ??? ??? ???? ?????.
2. ??? ?? ? ???({}) ?? ??? ???? ??? ? ????.
3. ??? ??? var? ???? ????, ?? ???? ?????.
4. ?? ??? PHP ??? ??? ?????, ??? ???? ?????? ??? ???? ???? ? ????.
<?php class Site { /* 成員變量 */ var $url; var $title; /* 成員函數(shù) */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title; } } //注:變量 $this 代表自身的對(duì)象。 ?>
?? ?? in php
???? ??? ? new ???? ???? ? ???? ??? ?????? ? ????.
$php = new Site;
$taobao = new Site;
$google = new Site;
? ???? ? ?? ??? ????? ? ??? ?? ??????
<?php header("Content-type: text/html; charset=utf-8");//設(shè)置編碼 class Site { /* 成員變量 */ var $url; var $title; /* 成員函數(shù) */ 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; // 調(diào)用成員函數(shù),設(shè)置標(biāo)題和URL $php->setTitle( "php中文網(wǎng)" ); $taobao->setTitle( "淘寶" ); $google->setTitle( "Google 搜索" ); $php->setUrl( 'ipnx.cn' ); $taobao->setUrl( 'www.taobao.com' ); $google->setUrl( 'www.google.com' ); // 調(diào)用成員函數(shù),獲取標(biāo)題和URL $php->getTitle(); $taobao->getTitle(); $google->getTitle(); $php->getUrl(); $taobao->getUrl(); $google->getUrl(); ?>
???
???? ??? ?????. ?? ??? ??? ? ??? ????? ? ?????. ?, ?? ?? ??? ???? ???? ? ?????. ?? new ???? ?? ?????.
PHP 5??? ???? ????? ???? ???? ??? ? ????. ?? ??? ??? ????.
void __construct ([ Mixed $args [, $... ]] )
?? ???
???(destructor) ???? ??? ??? ?? ??? ???? ??(?: ??? ???? ???? ???? ???? ?????.
PHP 5??? ?? ???? ??? ??? ??? ??? ??????. void __destruct. ( void )
<?php header("Content-type: text/html; charset=utf-8");//設(shè)置編碼 class MyDestructableClass { function __construct() { print "構(gòu)造函數(shù)</br>"; $this->name = "MyDestructableClass"; } function __destruct() { print "銷毀 " . $this->name; } } $obj = new MyDestructableClass(); ?>
??
PHP? ???? ???? ?? ?? ???? ?????. ??? ??? ????.
??? ?? ?? ?? {
???????????????????????????????????????????????????????????????????????????????
}
<?php // 子類擴(kuò)展站點(diǎn)類別 class Child_Site extends Site { var $category; function setCate($par){ $this->category = $par; } function getCate(){ echo $this->category; } } ?>
?? ????? ??? ???? ???? ?? ??? ??? ? ?? ?? ?? ???? ?? ?? ??? ? ????. ? ????? ??? ????? ???. getUrl ? getTitle ???? ?????? ?? ???????.
<?php function getUrl() { echo $this->url . PHP_EOL; return $this->url; } function getTitle(){ echo $this->title . PHP_EOL; return $this->title; } ?>
??? ??
??(Public): Public ??? ??? ????? ??? ?????.
protected (???): ??? ??? ??? ?????, ?? ?? ???? ?? ????? ???? ? ????. private (private): Private ??? ??? ??? ??? ?????? ???? ? ????. ?? ??? ?? ??? ??? ??, ??, ??? ? ??? ????? ???. var? ???? ??? ????? ???? ??? ?? ???? ???? ?? ?? ???? ??? ? ????. ?????. ??? ???? ???? ?? ?? ??? ???? public Constant ???? ??? ? ????. ?? ???? ???? ?? ???? ?????. ??? ???? ??? ? $ ??? ??? ??? ????. ??? ?? ??? ???? ?? ??, ??? ??, ?? ??? ?? ?? ?? ??? ? ? ????. PHP 5.3.0?? ??? ???? ???? ???? ??? ? ????. ??? ? ??? ?? ???(?: self, parent ?? static)? ? ????. ?? ??? ?? ???? ?????. inside ??? ??? ???? abstract? ??? ?? ?? ???? abstract? ????? ???. ???? ??? ???? ?????? ? ????. ???? ??? ???? ?? ???(????)? ???? ?? ?? ??? ??? ?? ????. ?? ???? ??? ? ?? ???? ?? ???? ?? ?? ???? ???? ??, ??? ???? ??? ??? ?? ???? ????? ? ????? ???. ?? ?? ?? ???? protected? ??? ?? ?? ???? ??? ???? protected ?? public?? ????? ?? private?? ??? ? ????. ?? ???? ???? ???? ???? ???. ?, ?? ????? ??? ??? ????? ???. ?? ?? ?? ???? ??? ?? ??? ????? ?? ???? ?? ??? ??? ???? ?? ?? ? ?? ??? ??? ????. ????? ?????? ???? ???? ???? ?? ???? ??? ? ??? ??? ???? ???? ??? ??? ??? ????. ?????? ?? ??? ??? ????? ????? ???? ?? ?????, ? ?? ??? ???? ?? ?? ????. ?????? ??? ?? ???? ????? ???. ?? ?????? ?????. ?????? ????? Implements ???? ?????. ???? ?????? ??? ?? ???? ???? ???. ??? ??? ???? ??? ?????. ???? ?? ?????? ??? ? ????. ??? ???? ?? ?????? ??? ?????. <?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; // 這行會(huì)產(chǎn)生一個(gè)致命錯(cuò)誤
//echo $obj->private; // 這行也會(huì)產(chǎn)生一個(gè)致命錯(cuò)誤
$obj->printHello(); // 輸出 Public、Protected 和 Private
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// 可以對(duì) 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; // 這行會(huì)產(chǎn)生一個(gè)致命錯(cuò)誤
$obj2->printHello(); // 輸出 Public、Protected2 和 Undefined
?>
<?php
/**
* Define MyClass
*/
class MyClass
{
// 聲明一個(gè)公有的構(gòu)造函數(shù)
public function __construct() { }
// 聲明一個(gè)公有的方法
public function MyPublic() { }
// 聲明一個(gè)受保護(hù)的方法
protected function MyProtected() { }
// 聲明一個(gè)私有的方法
private function MyPrivate() { }
// 此方法為公有
function Foo()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate();
}
}
$myclass = new MyClass;
$myclass->MyPublic(); // 這行能被正常執(zhí)行
//$myclass->MyProtected(); // 這行會(huì)產(chǎn)生一個(gè)致命錯(cuò)誤
//$myclass->MyPrivate(); // 這行會(huì)產(chǎn)生一個(gè)致命錯(cuò)誤
$myclass->Foo(); // 公有,受保護(hù),私有都可以執(zhí)行
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// 此方法為公有
function Foo2()
{
$this->MyPublic();
$this->MyProtected();
// $this->MyPrivate(); // 這行會(huì)產(chǎn)生一個(gè)致命錯(cuò)誤
}
}
$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
?>