PHP闈㈠悜瀵硅薄瀛︿範涔嬩簩:娣卞叆浜嗚В闈㈠悜瀵硅薄楂樼骇鐗规
闈欐佹柟娉(static)鍜屽睘鎬э細閫氳繃绫昏屼笉鏄璞℃潵璁块棶鏁版嵁鍜屽姛鑳
闈欐佹柟娉曟槸浠ョ被浣滀负浣滅敤浜庣殑鍑芥暟锛屼笉鑳借闂繖涓被涓殑鏅氬睘鎬э紝鍥犱负閭d簺灞炴у睘浜庝竴涓璞★紝浣嗗彲浠ヨ闂潤鎬佸睘鎬с

濡傛灉淇敼浜嗕竴涓潤鎬佸睘鎬э紝閭d箞杩欎釜绫荤殑鎵鏈夊疄渚嬮兘鑳借闂埌杩欎釜鏂板笺

渚嬪锛

print staticExample::$aNum;
StaticExample::sayHello();
瑕佺偣锛氶櫎闈炴槸璁块棶涓涓瑕嗗啓鐨勬柟娉曪紝璐熻矗姘歌繙鍙兘鐢::璁块棶琚槑纭0鏄庝负static鐨勬柟娉曞拰灞炴с

鈶狅細涓嶈兘鍦ㄥ璞′腑璋冪敤闈欐佹柟娉

鈶★細涓嶈兘鍦ㄩ潤鎬佹柟娉曚腑浣跨敤浼彉閲$this

<?php
/**
 * 闈欐佹柟娉曞拰灞炴э細閫氳繃绫昏屼笉鏄璞℃潵璁块棶鏁版嵁鍜屽姛鑳
 * =============娉ㄨВ
 * 鍙湁浣跨敤parent鍏抽敭瀛楄皟鐢ㄦ柟娉曟椂锛屾墠鑳藉涓涓潪闈欐佹柟娉曡繘琛岄潤鎬佸舰寮忚皟鐢(涓涓瓙绫诲彲浠ヤ娇鐢╬arent鍏抽敭瀛楄闂埗绫伙紝self鍏抽敭瀛椾粠褰撳墠绫讳腑璁块棶闈欐佹柟娉曟垨灞炴) 
 */

/**
 * 鏋勫缓shopProduct绫荤殑涓涓潤鎬佹柟娉曟潵鑷姩瀹炰緥鍖杝hopProduct瀵硅薄(鍦ㄤ笂鑺傜殑shopProduct绫诲熀纭涓婂鍔)
 * @return object shopProduct瀵硅薄
 */
 class shopProduct{
  private $id=0;
  //涓婅妭绫讳腑鐨勫唴瀹
  //...
  public function setID($id){
  $this->id=$id;
  }
  public static function getInstance($id, PDO $pdo){
  $stmt=$pdo->prepare("select * from products where id=?");
  $result=$stmt->execute(array($id));
  $row=$stmt->fetch();
  //瀹炰緥鍖朇D绫
  $product=new CDProudct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['playlength']);
  $product->setId($row['id']);
  $product->setDiscount($row['dusciybt']);
  return $product;
  }
 }
 /*
  * 杩欐牱鐨勬柟娉曟湁鐐瑰儚 宸ュ巶锛屽彲浠ユ帴鍙楀師濮嬫暟鎹垨閰嶇疆 鎹浜х敓瀵硅薄
  */
 $dsn='sqlite://home/db/bob/projects/products.db';
 $pdo=new PDO($dsn, null, null);
 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $obj=shopProduct::getInstance(1, $pdo);
 
鎶借薄绫(abstract class)鍜屾帴鍙(interface)锛氳璁″拰鐜板疄鐩稿垎绂
鎶借薄绫讳笉鑳界洿鎺ヨ瀹炰緥鍖栵紝鍙畾涔(娲婚儴鍒嗗疄鐜)瀛愮被闇瑕佺殑鏂规硶锛屽瓙绫诲彲浠ョ户鎵垮畠骞朵笖閫氳繃瀹炵幇鍏朵腑鐨勬娊璞℃柟娉曪紝浣挎娊璞$被鍏蜂綋鍖栥

鎶借薄绫昏嚦灏戝寘鍚竴涓娊璞℃柟娉

abstract class shopProductWriter{
    protected $products=array();
    abstract public function write();
}
<?php
/**
 * 鎶借薄绫诲疄渚
 * @author lxm
 */
abstract class shopProductWriter{
protected $products=array();
public function addProduct(shopProduct $shopProduct){
$this->products[]=$shopProduct;
}
abstract public function write();
}
/**
 * 杈撳嚭XML
 */
class xmlProductWriter extends shopProductWriter{
public function write(){
$str='<?xml version="1.0" encoding="UTF-8"?>'."\n";
$str.="<products>\n";
foreach ($this->products as $shopProduct){
$str.="\t<product title=\"".$shopProduct->getTitle()."\">\n";
//...
}
$str.="</products>\n";
}
}
鎶借薄绫绘彁渚涗簡鍏蜂綋瀹炵幇鐨勬爣鍑嗭紝鑰屾帴鍙(interface)鍒欐槸绾补鐨勬ā鏉裤傛帴鍙e彧鑳藉畾涔夊姛鑳斤紝鑰屼笉鍖呭惈瀹炵幇鐨勫唴瀹

鎺ュ彛鍙互鍖呭惈灞炴у拰鏂规硶澹版槑锛屼絾鏄柟娉曚负绌

渚嬪锛

interface Chargeable{
  public function getPrice();
}
class shopProduct implements Chargeable{
  //...
  public function getPrice(){
    return ;//...
  }
}
鎷︽埅鍣ㄦ柟娉曪細鑷姩濮旀墭
PHP鎻愪緵鍐呯疆鎷︽埅鍣╥nterceptor鏂规硶锛屽彲浠 鎷︽埅 鍙戦佸埌鏈畾涔夊彂鏀惧拰灞炴х殑娑堟伅銆

__get($property)       璁块棶鏈畾涔夌殑灞炴ф椂琚皟鐢

__set($property,$value)  缁欐湭瀹氫箟鐨勫睘鎬ц祴鍊兼椂琚皟鐢

__isset($property)   瀵规湭瀹氫箟鐨勫睘鎬ц皟鐢╥sset()鏃惰璋冪敤

__unset($property) 瀵规湭瀹氫箟鐨勫睘鎬ц皟鐢╱nset()鏃惰璋冪敤

__call($method,$arg_array)   璋冪敤鏈畾涔夌殑鏂规硶鏃惰璋冪敤

<?php
/**
 * 浣跨敤鎷︽埅鍣 璁块棶鏈畾涔夊睘鎬ф椂锛宊_get()琚皟鐢
 * 濡傛灉涓嶅瓨鍦ㄤ粈涔堜篃涓嶅仛锛岀敤鎴疯瘯鍥捐闂殑灞炴ц瑙f瀽涓篘ULL
 */
class Person{
function __get($property){
$method="get".$property;
if(!method_exists($this, $method)){
return $this->$method();
}
}
function getName(){
return "Bob";
}
function getAge(){
return 24;
}
}
$p= new Person();
print $p->name;
鏋愭瀯鏂规硶锛氬璞¢攢姣佸墠鐨勬竻鐞嗗伐浣
鍦ㄥ璞¤鍨冨溇鏀堕泦鍣ㄦ敹闆嗗墠(鍗冲璞′粠鍐呭瓨涓垹闄や箣鍓)鑷姩璋冪敤銆

<?php
/**
 * 闇瑕佹妸鑷韩淇℃伅鍐欏叆鏁版嵁搴擄紝鐢ㄦ瀽鏋勬柟娉曞湪瀵硅薄瀹炰緥琚垹闄ゆ椂纭繚瀹炰緥鎶婅嚜宸变繚瀛樺埌鏁版嵁搴撲腑
 */
class Person{
private $name;
private $age;
private $id;
function __construct($name,$age){
$this->name=$name;
$this->age=$age;
}
function setID($id){
$this->id=$id;
}
function __destruct(){
if(!empty($this->id)){
//淇濆瓨Person鏁版嵁 
print "saving person\n";
}
}
}
$person=new Person("bob", 24);
$person->setID(111);
unset($person);
//杈撳嚭
//淇濆瓨Person
鍥炶皟锛氱敤鍖垮悕鍑芥暟涓虹粍浠舵坊鍔犲姛鑳

浠ヤ笂灏辨槸PHP闈㈠悜瀵硅薄瀛︿範涔嬩簩:娣卞叆浜嗚В闈㈠悜瀵硅薄楂樼骇鐗规х殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=136