Blogger Information
Blog 37
fans 0
comment 0
visits 14213
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP(include,require;魔术方法)
秋闲独醉
Original
378 people have browsed it
  1. include, require的区别与联系, 与当前脚本的作用域的关系
  1. <?php
  2. // include './array_demo1.php';
  3. // echo "array_demo1.php'不存在会提示警告,会继续执行下面的语句";
  4. // require './array_demo2.php';
  5. // echo 'array-demo1.php不存在会给致命错误,终止程序执行';
  6. //public 公开的
  7. //private 私有的
  8. //protected 受保护的
  9. class ParentClass{
  10. public $username;
  11. protected $age;
  12. private $sex;
  13. public function __construct(array $arr){
  14. $this->username = $arr['username'];
  15. $this->age = $arr['age'];
  16. $this-> sex = $arr['sex'];
  17. }
  18. protected function run(){
  19. return $this->username.'在跑步';
  20. }
  21. private function sleep(){
  22. return $this->username.'在睡觉';
  23. }
  24. }
  25. $arr = ['username' => 'Tom','age' =>18,'sex' => '女'];
  26. $obj = new ParentClass($arr);
  27. var_dump(get_object_vars($obj)); //'username' => string 'Tom',在全局中只能访问公开的成员
  28. var_dump(get_class_methods($obj));//0 => string '__construct',在全局中只能访问公开的成员
  29. class SunClass extends ParentClass{
  30. public function __get($name)
  31. {
  32. return $this->$name;
  33. }
  34. public function all(){
  35. return get_class_vars('SunClass');
  36. }
  37. }
  38. $obj1 = new SunClass($arr);
  39. echo $obj1->username; //子类可以访问父类的公有成员
  40. echo $obj1->age;//子类可以访问父类的受保护成员
  41. // echo $obj->sex;//子类不可以访问父类的私有成员
  1. php类与对象的常用关键字,操作,以及几个魔术方法演示
  1. <?php
  2. namespace demo;
  3. class ParentClass{
  4. public function magic(){
  5. echo '__METHOD__返回类中方法名:'.__METHOD__.'<br>';
  6. echo '__CLAS__返回当前类名:'.__CLASS__.'<br>';
  7. echo '__FUNCTION__返回当前函数名:'.__FUNCTION__.'<br>';
  8. echo '__NAMESPACE__返回当前文件的空间名:'.__NAMESPACE__.'<br>';
  9. }
  10. }
  11. echo '__DIR__返回文件路径的目录部分:'. __DIR__.'<br>';
  12. echo '__FILE__返回文件的路径:'.__FILE__.'<br>';
  13. echo '__LINE__返回当前行的行号:'.__LINE__.'<br>';
  14. $obj = new ParentClass();
  15. $obj->magic();
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!