Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
include require
include引入方式
require引入方式
class
private
、公共成员public
、静态成员static
new Demo()
访问类成员$demo->user
使用->方式__get
读取私有变量,__set
修改变量__construct
构造方法此方法会在实例化时自动调用代码演示
<?php
namespace _0813;
class User
{
//声明私有变量
private $age = 18;
private $gongzi = 2800;
//声明公共变量
public $username = '老王';
//方法
public function getAge(){
return $this->age;
}
//魔术方法读
public function __get($name){
return $this->$name;
}
//魔术方法写
public function __set($name,$v){
$this->$name = $v;
}
}
$user = new User();
echo $user->username.'<br>';
echo $user->getAge().'<br>';
echo $user->gongzi.'<br>';
$user->gongzi = 3800;
echo $user->gongzi.'<hr>';
//类中的构造方法
class User1
{
private $chengji;
private $age;
public $username;
//静态方法
public static $guoji;
//魔术方法实例化类时自动调用
public function __construct($chengji,$age,$username,$guoji)
{
$this->chengji = $chengji;
$this->age = $age;
$this->username =$username;
//使用关键字self初始化静态属性
self::$guoji = $guoji;
}
public function __get($name){
return $this->$name;
}
}
$user1 = new User1('优秀',38,'小刘','中国');
echo '我的名字:'.$user1->username.',我的年龄是:'.$user1->age.',我的祖国是:'.$user1::$guoji;