Heim  >  Artikel  >  Backend-Entwicklung  >  回调:用匿名函数为组件添加功能

回调:用匿名函数为组件添加功能

WBOY
WBOYOriginal
2016-06-13 10:57:35865Durchsuche

PHP面向对象学习之二:深入了解面向对象高级特性

静态方法(static)和属性:通过类而不是对象来访问数据和功能

静态方法是以类作为作用于的函数,不能访问这个类中的普通属性,因为那些属性属于一个对象,但可以访问静态属性。

如果修改了一个静态属性,那么这个类的所有实例都能访问到这个新值。

例如:

print staticExample::$aNum;StaticExample::sayHello();

要点:除非是访问一个被覆写的方法,负责永远只能用::访问被明确声明为static的方法和属性。

①:不能在对象中调用静态方法

②:不能在静态方法中使用伪变量$this

<?php /** * 静态方法和属性:通过类而不是对象来访问数据和功能 * =============注解 * 只有使用parent关键字调用方法时,才能对一个非静态方法进行静态形式调用(一个子类可以使用parent关键字访问父类,self关键字从当前类中访问静态方法或属性)  *//** * 构建shopProduct类的一个静态方法来自动实例化shopProduct对象(在上节的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(); 		//实例化CD类 		$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.="</product></products>\n";	}}


抽象类提供了具体实现的标准,而接口(interface)则是纯粹的模板。接口只能定义功能,而不包含实现的内容

接口可以包含属性和方法声明,但是方法为空

例如:

interface Chargeable{  public function getPrice();}

class shopProduct implements Chargeable{  //...  public function getPrice(){    return ;//...  }}

拦截器方法:自动委托

PHP提供内置拦截器interceptor方法,可以 拦截 发送到未定义发放和属性的消息。

__get($property)       访问未定义的属性时被调用

__set($property,$value)  给未定义的属性赋值时被调用

__isset($property)   对未定义的属性调用isset()时被调用

__unset($property) 对未定义的属性调用unset()时被调用

__call($method,$arg_array)   调用未定义的方法时被调用

<?php /** * 使用拦截器 访问未定义属性时,__get()被调用 * 如果不存在什么也不做,用户试图访问的属性被解析为NULL */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

回调:用匿名函数为组件添加功能


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:求靠山权限设计思路Nächster Artikel:求一段正则,该如何处理