类的自动加载

  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();

更多相关文章

  1. 熔断原理与实现Golang版
  2. 百度静态网站全站搜索功能
  3. 【PHP 面向对象】面向对象(OOP)编程之魔术方法实现重载知识点归
  4. 打造立体化监控体系与APM最佳实践系列 --Zipkin部署与使用
  5. PHP:oop->抽象类/接口/后期静态绑定/单例模式连接数据库 Db类中
  6. Django 静态文件模板路径配置
  7. PHP:OOP基础/类(对象抽象化的结果)与对象 (类实例化结果)/构造方
  8. 【大家的项目】Rust Base62 库学习和分析
  9. php之类与继承

随机推荐

  1. android中include标签的使用
  2. Android终于公布源代码
  3. android中悬浮显示可拖动的歌词栏
  4. Android 编程好书推荐
  5. coco2d-x android
  6. 常用的android权限配置和常用工具代码
  7. android 设置 永不休眠
  8. Android 使用Json实现服务器与客户端数据
  9. Android(安卓)Snippet
  10. android AppCompatEditText 样式(线条粗细