您好,欢迎访问一九零五行业门户网

php面向对象接口,继承,抽象类,析构,克隆等高级特性实例详解

这篇文章主要介绍了php面向对象程序设计高级特性,结合实例形式分析了php面向对象程序设计中所涉及的静态属性、常量属性、接口、继承、抽象类、析构、克隆等概念与使用技巧,需要的朋友可以参考下
1. 静态属性
<?php class staticexample { static public $anum = 0; // 静态共有属性 static public function sayhello() { // 静态共有方法 print "hello"; } } print staticexample::$anum; staticexample::sayhello(); ?>
输出:0 hello
点评:静态属性和方法,可以通过类直接调用。
2. self
<?php class staticexample { static public $anum = 0; static public function sayhello() { // 这里的static 和 public的顺序可以颠倒 self::$anum++; print "hello (".self::$anum.")\n"; // self 指向当前类, $this指向当前对象。 } } staticexample::sayhello(); staticexample::sayhello(); staticexample::sayhello(); ?>
输出:
hello (1) hello (2) hello (3)
点评:self 指向当前类, this指向当前对象。self可以调用当前类的静态属性和方法。this指向当前对象。self可以调用当前类的静态属性和方法。this可以调用当前类的正常属性和方法。
3. 常量属性
<?php class shopproduct { const available = 0; // 只能用大写字母命名常量 const out_of_stock = 1; public $status; } print shopproduct::available; ?>
输出:0
点评:常量只能用大写字母,并且可以通过类直接调用。
4. 接口
<?php interface chargeable { // 接口,抽象类是介于基类与接口之间的东西 public function getprice(); } class shopproduct implements chargeable { // ... protected $price; // ... public function getprice() { return $this->price; } // ... } $product = new shopproduct(); ?>
如果没有实现getprice方法,将会报错。
fatal error: class shopproduct contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (chargeable::getprice)
5. 继承类与接口
<?php class timedservice{ } interface bookable{ } interface chargeable{ } class consultancy extends timedservice implements bookable, chargeable { // 继承类与接口 // ... } ?>
6. 抽象类
先来看一段代码
<?php abstract class domainobject { } class user extends domainobject { public static function create() { return new user(); } } class document extends domainobject { public static function create() { return new document(); } } $document = document::create(); print_r( $document ); ?>
输出:
document object ( )
7. 静态方法
<?php abstract class domainobject { private $group; // 私有属性group public function construct() { $this->group = static::getgroup();//static 静态类 } public static function create() { return new static(); } static function getgroup() { // 静态方法 return "default"; } } class user extends domainobject { } class document extends domainobject { static function getgroup() { // 改变了内容 return "document"; } } class spreadsheet extends document { // 继承之后,group也就与document相同了 } print_r(user::create()); print_r(spreadsheet::create()); ?>
输出:
user object ( [group:domainobject:private] => default ) spreadsheet object ( [group:domainobject:private] => document )
7. final字段
使类无法被继承,用的不多
<?php final class checkout { // 终止类的继承 // ... } class illegalcheckout extends checkout { // ... } $checkout = new checkout(); ?>
输出:
fatal error: class illegalcheckout may not inherit from final class (checkout)
final方法不能够被重写
<?php class checkout { final function totalize() { // calculate bill } } class illegalcheckout extends checkout { function totalize() { // 不能重写final方法 // change bill calculation } } $checkout = new checkout(); ?>
输出:
fatal error: cannot override final method checkout::totalize()
8. 析构函数
<?php class person { protected $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 ) ) { // save person data print "saving person\n"; } if ( empty( $this->id ) ) { // save person data print "do nothing\n"; } } } $person = new person( "bob", 44 ); $person->setid( 343 ); $person->setid( '' ); // 最后执行析构函数,使用完之后执行 ?>
输出:
do nothing
9. clone方法
克隆的时候执行
<?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 clone() { // 克隆时候执行 $this->id = 0; } } $person = new person( "bob", 44 ); $person->setid( 343 ); $person2 = clone $person; print_r( $person ); print_r( $person2 ); ?>
输出:
person object ( [name:person:private] => bob [age:person:private] => 44 [id:person:private] => 343 ) person object ( [name:person:private] => bob [age:person:private] => 44 [id:person:private] => 0 )
再看一个例子
<?php class account { // 账户类 public $balance; // 余额 function construct( $balance ) { $this->balance = $balance; } } class person { private $name; private $age; private $id; public $account; function construct( $name, $age, account $account ) { $this->name = $name; $this->age = $age; $this->account = $account; } function setid( $id ) { $this->id = $id; } function clone() { $this->id = 0; } } $person = new person( "bob", 44, new account( 200 ) ); // 以类对象作为参数 $person->setid( 343 ); $person2 = clone $person; // give $person some money $person->account->balance += 10; // $person2 sees the credit too print $person2->account->balance; // person的属性account也是一个类,他的属性balance的值是210 // output: // 210 ?>
点评:学习还是能够开拓大脑的,今天终于明白为什么有多个箭头的概念了$person->account->balance。这里的account属性是一个对象。
10. tostring
<?php class person { function getname() { return "bob"; } function getage() { return 44; } function tostring() { $desc = $this->getname()." (age "; $desc .= $this->getage().")"; return $desc; } } $person = new person(); print $person; // 打印时候集中处理 // bob (age 44) ?>
点评:必须是print或echo时才有效,print_r就输出对象。
以上就是php面向对象接口,继承,抽象类,析构,克隆等高级特性实例详解的详细内容。
其它类似信息

推荐信息