首页 > 其他 > 详细

类的静态变量访问

时间:2016-04-17 00:22:57      阅读:228      评论:0      收藏:0      [点我收藏+]
<?php

/**
 * @author keke
 * @copyright 2016
 * 这是一个类的标准格式,用于记忆,静态变量访问:student::$fee
 */

class student
{
    public static $fee = 0;
    public $name;
    function __construct($name)
    {
        $this->name = $name; //非静态属性访问格式:$this->name
    }

    public static function runCode($fee)
    {
        student::$fee += $fee; //这里不能用$this->fee非静态格式访问,是静态变量访问格式:student::$fee
    }

    public function returnStr()
    {
        return student::$fee; //这里不能用$this->fee非静态格式访问,是静态变量访问格式:student::$fee
    }

}

$zhangsan = new student("张三");
$zhangsan->runCode(320);

$lisi = new student("李四");
$lisi->runCode(560);
//这里不能用( 类::方法 )来访问
echo $lisi->returnStr(); //结果:880

echo ‘<br />运行完成!‘;
?>

 

类的静态变量访问

原文:http://www.cnblogs.com/qingsong/p/5399809.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!