<?php
//对象复制
class MyCloneable {
static $id = 0;
function MyCloneable() {
$this->id = self::$id++;
}
/*
function __clone() {
$this->address = "New York";
$this->id = self::$id++;
}
*/
}
$obj = new MyCloneable();
$obj->name = "Hello";
$obj->address = "Tel-Aviv";
print $obj->id . "n";
$obj_cloned = clone $obj;
print $obj_cloned->id . "n";
print $obj_cloned->name . "n";
print $obj_cloned->address . "n";
?>
以上代码复制出一个完全相同的对象。
然后请把function __clone()这一个函数的注释去掉,重新运行程序。则会复制出一个基本相同,但部份属性变动的对象。
8) ★ Class Constants 类常量
PHP5中可以使用const关键字来定义类常量。
<?php
class Foo {
const constant = "constant";
}
echo "Foo::constant = " . Foo::constant . "n";
?>
11) ★__METHOD__ constant __METHOD__常量
__METHOD__ 是PHP5中新增的“魔术”常量,表示类方法的名称。
魔术常量是一种PHP预定义常量,它的值可以是变化的,PHP中的其它已经存在的魔术常量有__LINE__、__FILE__、__FUNCTION__、__CLASS__等。
<?php
class Foo {
function show() {
echo __METHOD__;
}
}
class Bar extends Foo {
}
Foo::show(); // outputs Foo::show
Bar::show(); // outputs Foo::show either since __METHOD__ is
// compile-time evaluated token
function test() {
echo __METHOD__;
}
test(); // outputs test
?>
(出处:Viphot)







