博主信息
博文 19
粉丝 1
评论 0
访问量 16409
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
0506作业
▽空城旧梦
原创
1057人浏览过

类的自动加载

  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举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学