博客列表 >0506作业

0506作业

▽空城旧梦
▽空城旧梦原创
2021年05月18日 07:50:53805浏览

类的自动加载

  1. <?php
  2. spl_autoload_register(function($class){
  3. require $class.'.php';
  4. });

类的基本形式

  1. class Player{
  2. public $name = 'Jordan';
  3. public $height;
  4. public $team;
  5. protected $playerNum;
  6. public function __construct($name,$height,$team,$weight){
  7. $this->name = $name;
  8. $this->height = $height;
  9. $this->team = $team;
  10. $this->weight = $weight;
  11. }
  12. public function jog(){
  13. return "$this->name is jogging, weighing $this->weight<br>";
  14. }
  15. public function shoot(){
  16. return "$this->name is shooting, his height is $this->height<br>";
  17. }
  18. }

类的调用

  1. $np2 = new Player('kobe','206cm','Laker',"85kg");
  2. echo $np2->name;
  3. echo $np2->shoot();

类的静态成员

  1. class User
  2. {
  3. public static $name = '胡歌';
  4. protected $_config = [
  5. 'auth_on'=>'true',
  6. 'auth_type'=>1,
  7. ];
  8. public static $nation = "China";
  9. private static $salary ;
  10. static $count = 0;
  11. public function __construct($name,$salary){
  12. self::$name = $name;
  13. self::$salary = $salary;
  14. self::$count++;
  15. }
  16. public function getConfig(){
  17. return sprintf('认证开关:%s<br>,认证类型:%d',$this->_config['auth_on'],$this->_config['auth_type']);
  18. }
  19. public static function getCount()
  20. {
  21. return sprintf('User类被实例化了%d次<br>',self::$count);
  22. }

类的静态成员调用

  1. ECHO $user2->getCount();
  2. echo User::getCount();
  3. echo User::$name;
  4. echo $user2->getConfig();
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议