|
本帖最后由 _only 于 2023-2-11 20:13 编辑
__clone()方法应用实例
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}body {
margin-left:10px;
margin-top: 10px;
margin-right:10px;
margin-bottom: 10px;
}
-->
</style>
<?php
class Myobject{ //类Myobject
private $object_type = 'book'; //声明私有变量$objuect_type,并赋初值等于“book"
public function setType($type){
$this -> objuect_type = $type;
}
public function getType(){
return $this -> object_type;
}
public function __clone(){
$this ->object_type = 'computer';
}
}
$book1 = new Myobject();
$book2 = clone $book1;
echo '对象$book1的变量值为:'.$book1 -> getType();
echo '<br>';
echo '对象$book2的变量值为:'.$book2 -> getType();
?>
执行结果:
对象$book1的变量值为:book
对象$book2的变量值为:computer
|
|