批改状态:合格
老师批语:文件 加载, 好好学吧, 后面就全是自动加载了, 连写include的机会都没有了
1、代码练习
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>测试网页</title></head><body><?php include_once 'public_header.php';?><?php include_once 'public_header.php';?><!-- include_once和require_once去重加载 --><?php @include_once 'public_head.php';?><!--'@'抑制错误 --><?php echo 'include出现错误还继续执行输出'; ?><?php require('public.php'); ?><?php echo 'require继续执行输出','<br>'; ?><code><?php echo 'require出现错误不继续执行输出','<br>'; ?></code><?php echo file_exists('public.php') ? '文件存在':'文件不存在';echo '<br>';?><?php echo is_file('public.php') ? '是文件':'是文件夹';?><?php $file='public';require($file.'.php'); ?><?php echo '<hr>'; ?><?php include_once "test.php"; ?><?php $product="zhongyequan";echo $site,'<br>'; ?><?phpecho write($product);echo '<br>';function get(){include('test1.php');echo $site1;}echo '<br>';get();echo '<br>';echo $site1??'无法访问函数内部include()导入的变量';//函数作用域导入外部文件中的变量和方法外部无法访问?></body></html>
2、代码演示结果:
1、代码练习
<?php// 声明类:class 类名 {}class Name{//类中的属性public $name='ldy';// 类中的方法public function getname(){return $this->name;}}// 类实例化关键字:newecho (new Name)->getname();echo '<br>';$class=new Name();// 检测类型echo gettype($class),'<br>';// 检测类的名字echo get_class($class),'<br>';// 判断是否属于类 :boolecho ($class instanceof Name) ? '$class属于Name' : '$class不属于Name';echo '<br>';class A{public static $name='dachengzhongye';public $site='种业圈';public $arr=[1,2];public $count=10;public function getM(){return '当前类:'.__CLASS__.'->'.__METHOD__;}public static function getVar(){return get_class_vars(__CLASS__);}public static function get(){return get_class_vars(get_class(new self));}}print_r(A::getVar());echo '<br>';print_r(A::get());echo '<br>';echo A::$name;echo '<br>';$newclass=new A();echo $newclass->count;echo '<br>';echo $newclass->getM();
2、演示结果
1、加载文件关键字:include和require;
2、加载文夹去重关键字:include_once和require_once;
3、include和require的区别在于,出现错误include报错但继续执行
而require(强制加载)报错同时打断程序执行;
4、加载的文夹中的函数和变量与当前文夹内容同属一个作用域;
1、类由关键字class声明定义;由new关键字实例化
2、get_class():获取实例类的名字
3、$class instanceof Class:判断$class是否属于Class类:返回布尔值
4、类属性的值:不能用变量、类属性和方法、不能用表达式、不能用函数
5、::访问范围解析符;->对象运算符;
6、类静态属性可以直接类名字+::+静态属性来访问
7、类常见关键字:public\protected\private;静态static;抽象abstract;final;接口:interface;方法集:trait;
8、继承(扩展):extends继承类;implements继承接口;use调用方法集;
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号