Blogger Information
Blog 26
fans 0
comment 0
visits 18147
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件加载以及类的创建和访问
雪~人胖胖
Original
596 people have browsed it

文件加载

  • 文件加载: 可简单的理解为将外部文件内容复制到当前文档中
  • 根据被加载文件的特征和重要性,可分为: “条件加载” 和 “强制加载” 二种方式

1. 条件加载

加载外部文件,如果失败报警告级(Warning)错误,不会中断程序

序号 语法 描述
1 include 条件加载
2 include_once 去重(chong)条件加载

2. 强制加载

加载外部文件,如果失败报致命级(Fatal error)错误,并中断程序

序号 语法 描述
1 require 强制加载
2 require_once 去重强制加载
  1. <?php
  2. //include是条件加载文件,报错了脚本还是会执行下去
  3. include 'index.php';
  4. include "index.php";
  5. $var = "index.php";
  6. include $var;
  7. $var = "index";
  8. include $var .'.php';
  9. include $var1 ;
  10. //通常在加载文件前先要做个判断
  11. $file = 'index1.php';
  12. if(file_exists($file) && is_file($file)){
  13. include $file;
  14. }else{
  15. include 'default.php';
  16. }
  17. echo '程序没有终止';
  18. echo '<br>';
  19. //include_once仅加载一次
  20. include_once 'common.php';
  21. include_once 'common.php';
  22. echo demo();
  23. //require强制加载,如果加载失败终止当前脚本 主要用于加载重要的文件
  24. //require_once去重加载
  25. require "index2.php";
  26. echo '程序没有终止';

图例

类与对象

1. 概念

序号 名称 描述 关键字
1 class 对象的模板 class
2 对象instance 类的实例 new
  • 类与对象界限并不清晰,在不影响理解前提下,常用类代表实例来引用类成员
  • 对象与类的某一个实例绑定, 所以对象与实例可视为同义词,互换并不影响
  1. //创建类
  2. class User
  3. {
  4. //创建类属性:类中的变量
  5. //类中成员的作用域
  6. //1.常规属性:非静态属性/动态属性
  7. public $name = '张三';
  8. public $age = '21';
  9. public $grade = ['语文'=>80, '数学'=>90];
  10. //nowdoc
  11. public $output = <<<'VAR'
  12. <h3>中国必胜</h3>
  13. VAR;
  14. //heredoc解析特殊字符\n\r
  15. //非法属性值
  16. //不能使用变量
  17. //不能用类属性类方法
  18. //不能用表达式
  19. //不能用函数
  20. //2.静态属性
  21. //如果一个属性值对所有对象都一样,可以把这个属性用类访问
  22. public static $nationality = '中国/CHINA';
  23. //抽象属性 没有初始化
  24. public $salary;
  25. }
  26. //创建对象
  27. $user_info = new User;
  28. //修改值
  29. $user_info->name = '李四';
  30. echo "姓名:{$user_info->name} 年龄:{$user_info->age}";
  31. //修改静态属性
  32. User::$nationality = '英国/UK';
  33. //访问静态属性
  34. echo User::$nationality;

总结

1.加载重要的文件使用require进行加载,比如数据库连接,其余文件使用include进行加载如果,只加载一次使用require_once或者include_once
2.类是一个包含属性和方法的模板,属性就是变量,方法就是函数,把需要重复使用的变量和函数放在一个类中。使用这个类中的变量和函数,只需要把类实例化为对象,然后就可以访问类中的属性和方法。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:类是对象的模板, 接口是类的模板, 这样就好理解, 对不?
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!