Blogger Information
Blog 36
fans 1
comment 0
visits 26429
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
include和require的区别 实例演示:类与对象关键字 魔术方法
早晨
Original
1096 people have browsed it

include和require的区别

加载失败的处理方式不同
includerequire除了在处理引入文件的方式不同外,最大的区别就是:
include在引入不存在文件时产生一个警告且脚本还会继续执行,
require则会导致一个致命性错误且脚本停止执行。
例如:

  1. <?php
  2. include 'xxxx.php';
  3. echo 'hello';
  4. ?>

如果xxxx.php不存在,echo ‘world’这句是可以继续执行的。

  1. <?php
  2. require 'xxxx.php';
  3. echo 'hello';
  4. ?>

如果xxxx.php不存在,echo ‘hello’这句是不会执行的,到require时就停止了。

实例演示:类与对象关键字 魔术方法

  1. <?php
  2. namespace _0812;
  3. class Ab
  4. {
  5. public $name; //公有成员
  6. private $age = 25; //私有成员
  7. static $tel; // 静态成员
  8. public function __construct($name, $tel = '13000000000')
  9. {
  10. $this->name = $name;
  11. self::$tel = $tel;
  12. }
  13. }
  14. $abc = new Ab('早晨', '13000008888');
  15. echo '公有成员:' . $abc->name . '<br>';
  16. echo '静态成员:' . Ab::$tel . '<br>';
  17. // 私有成员赋值
  18. @$ages->age = 35;
  19. echo '私有成员:' . $ages->age . '<br>';
  20. echo '<hr>';
  21. class Cd
  22. {
  23. private $age = 30;
  24. public function __get($ages)
  25. {
  26. if ($ages === 'age') {
  27. return $this->$ages + 5;
  28. }
  29. }
  30. public function __set($ages, $value)
  31. {
  32. if ($ages === 'age') {
  33. if ($value >= 18 && $value <= 35) {
  34. $this->$ages = $value;
  35. } else {
  36. echo '您超过年龄了!';
  37. }
  38. }
  39. }
  40. }
  41. $user = new Cd();
  42. echo $user->age . '<br>';
  43. $user->age = 40;

运行效果

Correcting teacher:PHPzPHPz

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