时间:2021-07-01 10:21:17 帮助过:22人阅读
静态方法是以类作为作用于的函数,不能访问这个类中的普通属性,因为那些属性属于一个对象,但可以访问静态属性。
如果修改了一个静态属性,那么这个类的所有实例都能访问到这个新值。
例如:
print staticExample::$aNum; StaticExample::sayHello();
①:不能在对象中调用静态方法
②:不能在静态方法中使用伪变量$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 shopProductWriter{
protected $products=array();
abstract public function write();
}products[]=$shopProduct; } abstract public function write(); } /** *输出XML */ class xmlProductWriter extends shopProductWriter{ public function write(){ $str=''."\n"; $str.="
抽象类提供了具体实现的标准,而接口(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) 调用未定义的方法时被调用
$method();
}
}
function getName(){
return "Bob";
}
function getAge(){
return 24;
}
}
$p= new Person();
print $p->name;
在对象被垃圾收集器收集前(即对象从内存中删除之前)自动调用。
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