本文实例讲述了php面向对象程序设计高级特性。分享给大家供大家参考,具体如下:
静态属性
<?php
class staticexample {
static public $anum = 0; // 静态共有属性
static public function sayhello() { // 静态共有方法
print "hello";
}
}
print staticexample::$anum;
staticexample::sayhello();
?>
输出:0 hello
点评:静态属性和方法,可以通过类直接调用。
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可以调用当前类的正常属性和方法。
常量属性
<?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)
继承类与接口
<?php
class timedservice{ }
interface bookable{ }
interface chargeable{ }
class consultancy extends timedservice implements bookable, chargeable { // 继承类与接口
// ...
}
?>
抽象类
先来看一段代码
<?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
(
)
静态方法
<?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
)
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()
析构函数
<?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
__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属性是一个对象。
__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就输出对象。
person object()
希望本文所述对大家php程序设计有所帮助。
更多php面向对象程序设计高级特性详解(接口,继承,抽象类,析构,克隆等)。