php类的综合运用-继承、定义、实例化
时间:2010-07-28 来源:原创 作者:yuge 浏览次数:
面向对象课程即将结束,php类的综合运用-继承、定义、实例化、构造函数、自动调用函数(方法)定义、__get、__set、__isset、__unset、__tostring()、__call 运用、对象克隆。
class person{
private $name;
private $sex;
protected $age;
var $code;
public static $mycount="china";
const aa='const aa';
function __construct($name,$sex,$age,$code){
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
$this->code=$code;
}
public function __get($property){
if(isset($property)){
return $this->$property ;
}
else
{
return(NULL);
}
}
public function __set($property,$value){
$this->$property=$value;
}
function person()
{
$this->__construct();
}
function __isset($property){
return isset($this->$property);
}
function __unset($property){
unset($this->$property);
}
function __tostring(){
return $this->code;
}
function __call($function_name,$agre)//出现未定义的方法输出提示
{
echo "<br>you call function not define".$function_name."(argments:";
print_r($agre);
echo ")";
}
function say(){
echo 'name is:'.$this->name.' sex is:'.$this->sex.' age is:'.$this->age;
}
function run(){
echo '<br>i can run';
}
}
class student extends person //继承person类
{
var $school;
function __construct($name,$sex,$age,$code,$school){
person::__construct($name,$sex,$age,$code);
$this->school=$school;
}
function study(){
echo "i am in ".$this->school;
}
function say(){
person::say();//在类里面用类名和::调用类里的方法
person::$mycount='U.S';//在类里面用类名和::调用
echo 'im in '.$this->school.' im age:'.$this->age;
}
}
$p0=new person('shuaige','man',30,'heize');//new 一个基类对象
$p1=new student('zhangsan','boy',21,'1111233','santong');//new 一个子类的对象
$p0->say();
echo "<br/>";
$p1->study();
echo "<br/>";
$p1->say();
person::$mycount="U.s";
echo "<br>".person::$mycount;//用类名调用方法
echo "<br>".person::run();
echo student::aa."<br>";
print_r($p1);
$p1->aa('a','b');//调用未定义函数,触发错误
$p2=clone $p1;//克隆对象
echo "<br>".$p2->name;
echo "<br/>";
echo "<br>".$p2->say();
class person{
private $name;
private $sex;
protected $age;
var $code;
public static $mycount="china";
const aa='const aa';
function __construct($name,$sex,$age,$code){
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
$this->code=$code;
}
public function __get($property){
if(isset($property)){
return $this->$property ;
}
else
{
return(NULL);
}
}
public function __set($property,$value){
$this->$property=$value;
}
function person()
{
$this->__construct();
}
function __isset($property){
return isset($this->$property);
}
function __unset($property){
unset($this->$property);
}
function __tostring(){
return $this->code;
}
function __call($function_name,$agre)//出现未定义的方法输出提示
{
echo "<br>you call function not define".$function_name."(argments:";
print_r($agre);
echo ")";
}
function say(){
echo 'name is:'.$this->name.' sex is:'.$this->sex.' age is:'.$this->age;
}
function run(){
echo '<br>i can run';
}
}
class student extends person //继承person类
{
var $school;
function __construct($name,$sex,$age,$code,$school){
person::__construct($name,$sex,$age,$code);
$this->school=$school;
}
function study(){
echo "i am in ".$this->school;
}
function say(){
person::say();//在类里面用类名和::调用类里的方法
person::$mycount='U.S';//在类里面用类名和::调用
echo 'im in '.$this->school.' im age:'.$this->age;
}
}
$p0=new person('shuaige','man',30,'heize');//new 一个基类对象
$p1=new student('zhangsan','boy',21,'1111233','santong');//new 一个子类的对象
$p0->say();
echo "<br/>";
$p1->study();
echo "<br/>";
$p1->say();
person::$mycount="U.s";
echo "<br>".person::$mycount;//用类名调用方法
echo "<br>".person::run();
echo student::aa."<br>";
print_r($p1);
$p1->aa('a','b');//调用未定义函数,触发错误
$p2=clone $p1;//克隆对象
echo "<br>".$p2->name;
echo "<br/>";
echo "<br>".$p2->say();
版权归原作者所有,内容仅供参考学习,不得用于商业用途。