博客首页 注册 建议与交流 排行榜 加入友情链接         宝宝相册的专门空间
推荐 投诉 搜索: 帮助

流水孟春

以前使用的博客 webvista.cublog.cn
lib.cublog.cn
一个例子来看PHP5中的this,self和parent关键字

<?php
/**
 * $this调用本类或父类的非静态属性或方法,self调用本类的静态属性或方法,
 * parent用于调用父类的静态属性或方法
 */

class Exam{
    public $str="var1\n";
    public static $num=1;
    
    function __construct($var){
        return $this->str=$var;
    }
    
    /**
     * Enter description here...
     *
     * @return unknown
     */

    function myFunction(){
        return $this->str.="I'm Working\n";
    }
    
    /**
     * Enter description here...
     *
     * @return unknown
     */

    function __destruct(){
        print self::$num+=4;
        print "\n";
    }
}

class Exam2 extends Exam {
    public $var;
    function __construct($x){
        return $this->var=parent::$num+$x;
    }
}

$o1=new Exam("liushuimengchun!");
echo "\n";
echo $o1->myFunction();
echo "\n";
$o2=new Exam2(5);
echo "\n";
echo $o2->var;

/*
输出:
liushuimengchun!I'm Working


65
9
*/

?>

发表于: 2007-01-06,修改于: 2007-01-06 02:40,已浏览522次,有评论0条 推荐 投诉

给我留言
版权所有 ChinaUnix.net 页面生成时间:0.00957