Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
// include './array_demo1.php';
// echo "array_demo1.php'不存在会提示警告,会继续执行下面的语句";
// require './array_demo2.php';
// echo 'array-demo1.php不存在会给致命错误,终止程序执行';
//public 公开的
//private 私有的
//protected 受保护的
class ParentClass{
public $username;
protected $age;
private $sex;
public function __construct(array $arr){
$this->username = $arr['username'];
$this->age = $arr['age'];
$this-> sex = $arr['sex'];
}
protected function run(){
return $this->username.'在跑步';
}
private function sleep(){
return $this->username.'在睡觉';
}
}
$arr = ['username' => 'Tom','age' =>18,'sex' => '女'];
$obj = new ParentClass($arr);
var_dump(get_object_vars($obj)); //'username' => string 'Tom',在全局中只能访问公开的成员
var_dump(get_class_methods($obj));//0 => string '__construct',在全局中只能访问公开的成员
class SunClass extends ParentClass{
public function __get($name)
{
return $this->$name;
}
public function all(){
return get_class_vars('SunClass');
}
}
$obj1 = new SunClass($arr);
echo $obj1->username; //子类可以访问父类的公有成员
echo $obj1->age;//子类可以访问父类的受保护成员
// echo $obj->sex;//子类不可以访问父类的私有成员
<?php
namespace demo;
class ParentClass{
public function magic(){
echo '__METHOD__返回类中方法名:'.__METHOD__.'<br>';
echo '__CLAS__返回当前类名:'.__CLASS__.'<br>';
echo '__FUNCTION__返回当前函数名:'.__FUNCTION__.'<br>';
echo '__NAMESPACE__返回当前文件的空间名:'.__NAMESPACE__.'<br>';
}
}
echo '__DIR__返回文件路径的目录部分:'. __DIR__.'<br>';
echo '__FILE__返回文件的路径:'.__FILE__.'<br>';
echo '__LINE__返回当前行的行号:'.__LINE__.'<br>';
$obj = new ParentClass();
$obj->magic();