批改状态:合格
老师批语:
根据prs-4规范,类的名称与类的文件名要保持一直,所以定义一个类的名称及文件名时要注意保持相同,否则自动加载会出现问题。
创建一个Player类,此类我们用作为父类
<?php
//定义命名空间
namespace controller;
//定义一个类
class Player{
// 成员属性前一定要有访问修饰符 public protected private
public $name;
public $height;
public $team;
protected $playNum = 23;// protected 只能被本类和子类访问
private $weight;// 只能被本类访问
//__construct()魔术方法--构造方法:构造函数(构造器)除了可以给public公有成员初始化赋值外,还可以用来给私有成员,受保护的成员初始化赋值
public function __construct($name,$height,$team,$playNum,$weight){
//对象成员之间的内部访问可以用墙头草$this
$this->name = $name;
$this->height = $height;
$this->team = $team;
$this->playNum = $playNum;
$this->weight = $weight;
}
//声明两个方法
public function jog()
{
return "$this->name is jogging,whose playNum is $this->playNum<br>";
}
public function shoot()
{
return "$this->name is shooting,weighing $this->weight<br>";
}
}
?>创建一个CnPlayer子类,子类继承父类Player的所有的内容,子类中可以扩展新增属性跟方法,也可以重写父类的方法:
class CnPlayer extends Player
{
//新增一个属性
public $country;
//给子类创建构造函数(重写父类的构造函数),如果父类中的属性都能用到可以用parent::直接调用父类中的构造函数
public function __construct($name,$country)
{
//parent::直接调用父类中的构造函数
//parent::__construct($name,$height,$team,$playNum,$weight);
$this->name = $name;
$this->country = $country;
}
public function from()
{
return "{$this->name} come from {$this->country}";
}
}$zs = new CnPlayer('ZhangSiRui','China');
echo $zs->from();__construct()虽然方便,但是每次类实例化一次 就自动调用一次就多占用一份内存,一些需要频繁调用,可以用静态类static来实现,不管调用多少次,都只占用一份内存,但是
静态成员不自动进行销毁,而实例化的则可以销毁。
<?php
class User
{
public static $name = '胡歌';
protected $_config = [
'auth_on'=>'true',
'auth_type'=>1//认证方式 1 实时认证 2位登录认证
];
public static $nation = 'China';
private $salary;
static $count = 0;//记录一登录用户的总数
public function __construct($name,$salary)
{
//静态成员与类的实例无关,不能用$this访问,用self::类的引用 访问静态成员
self::$name = $name;
$this->salary = $salary;
self::$count ++;
}
//静态方法中调用静态属性(静态方法中不能用$this->调用普通属性)
public static function getCount(){
return sprintf('目前该网站的在线人数为%d<br>',self::$count);
}
public function getConfig()
{
return sprintf('认证开关:%s<br>,认证类型%d',$this->_config['auth_on'],$this->_config['auth_type']);
}
}
?>静态成员可以在未实例化的情况下就直接调用,因为静态成员都是类级别的存在:
<?php require 'User.php'; echo User::$name;
这个情况下是可以直接输出的,但是上面定义的__construct()中的$count++在未实例化类之前是不生效的,且静态成员不自动进行销毁,而实例化的则可以销毁:
<?php
require 'User.php';
$user = new User('古力娜扎',25000);
$user2 = new User('迪丽热巴',25000);
$user3 = new User('骚瑞',25000);//这里实例化三次 $count++也就执行了三次 输出$count会为3
echo User::$name;
//这里如果使用一个unset()销毁,则普通成员全部销毁,但静态成员不自动进行销毁
unset($user);
//echo $user->getConfig();//这里汇报一个致命错误,因为getConfig()方法已被销毁,现在已不存在
echo User::$name;//静态成员依旧可以正常输出autoload自动加载的时候需要考虑到命名空间的问题 Linux跟windows里面路径中的目录分隔符写法不同,如果实现一个类自动加载能在Linux跟windows系统都能使用,就需要做一个特殊处理。
<?php
spl_autoload_register(function($className){
//声明一个变量 赋值为用__DIR__获取当前文件位置拼接上类所在的文件夹位置再拼接上类名称加.php后缀的绝对路径
$file =__DIR__.'\\controller\\'. str_replace('\\',DIRECTORY_SEPARATOR,$className).'.php';
//判断是否是不是文件名且该文件是否不存在
if(!(is_file($file)&&file_exists($file)))
{
//如果为真 则抛出一个错误
throw new \Exception('文件名不合法或者不存在');
}
//为假就返回文件绝对路径
require $file;
});
?>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号