php面向对象 $this的使用

<?php
class Stu
{
    private $name = '明明';//私有属性
    private $salary = 3000;//私有属性

    protected function line(){ //受保护的方法
        return '<hr>';
    }

    public function fun(){
        $str = '学生的姓名是: '.$this->name; //$this 调用name
        $str .= '<br><i style="color:#999">来条华丽的分割线</i>'.$this->line();//$this调用line() 方法
        $str .= '工资是: '.$this->salary;//$this调用$salary属性
        echo $str;
    }
}

$obj= new Stu();
$obj->fun();//调用

 

原文地址:https://www.cnblogs.com/xm666/p/11252679.html