Blogger Information
Blog 45
fans 0
comment 0
visits 34627
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php 类成员重载 与命名空间
咸鱼老爷
Original
660 people have browsed it

类成员“重载”

  • 属性重载

    属性重载实际上就是属性访问一个拦截器,用来过滤用户的请求
    重载属性简单实现一个访问限制
    1. class Demo
    2. {
    3. private $role='admin';
    4. private $username='admin';
    5. //属性重载
    6. public function __get($name)
    7. {
    8. if($this->role === 'admin'){
    9. return $this->username;
    10. }else{
    11. return '非法访问';
    12. }
    13. }
    14. }
    15. $obj=new Demo();
    16. echo $obj->username;
    • 获取器

      通常会自动创建一个获取器,动态的生成一个方法名称
      1. class Demo
      2. {
      3. protected $data=[
      4. 'name'=>'admin',
      5. ];
      6. public function __get($name){
      7. $method='get'.ucfirst($name);
      8. if(method_exists($this,$method))return $this->$method($name);
      9. }
      10. public function getName($name){
      11. if(!isset($this->data[$name])){
      12. $this->data[$name]=0;
      13. }else{
      14. return $this->data[$name];
      15. }
      16. }
      17. }
      18. $obj=new Demo();
      19. echo $obj->name;
    • 修改器

      __set当访问不存在无权限设置的属性时自动调用
      1. class Demo
      2. {
      3. protected $data=[
      4. 'name'=>'admin',
      5. ];
      6. public function __set($name,$value){
      7. $method='set'.ucfirst($name);
      8. if(method_exists($this,$method))return $this->$method($name,$value);
      9. }
      10. public function setEmail($name,$value){
      11. $this->data[$name]=$value;
      12. }
      13. }
      14. $obj=new Demo();
      15. $obj->email='admin@php.cn';
      16. var_dump($obj);
  • 方法重载

    • 普通方法重载 __call

      1. public function __call($name, $arguments)
      2. {
      3. return $name.'<br>'.implode(',',$arguments);
      4. }
      5. $obj=new Demo();
      6. var_dump($obj->hello(1,2,3));

    • 静态方法重载 __callstatic

      1. public static function __callStatic($name, $arguments)
      2. {
      3. return $name.'<br>'.implode(',',$arguments);
      4. }
      5. $obj=new Demo();
      6. var_dump(Demo::hello(1,2,3));

      全局成员 函数,常量,类

      不能重复定义

      命名空间

      主要就是为了解决全局成员的命名冲突

      1. namespace app1{
      2. class Model{
      3. }
      4. const PATH='app/';
      5. function get(){
      6. }
      7. echo Model::class,'<br>';
      8. echo PATH::class,'<br>';
      9. echo get::class,'<br>';
      10. }
      11. namespace app2{
      12. class Model{
      13. }
      14. const PATH='app/';
      15. function get(){
      16. }
      17. echo Model::class,'<br>';
      18. echo PATH::class,'<br>';
      19. echo get::class,'<br>';
      20. }


      全局命名空间

      1. namespace{
      2. class Model{
      3. }
      4. const PATH='app/';
      5. function get(){
      6. }
      7. echo Model::class,'<br>';
      8. echo PATH::class,'<br>';
      9. echo get::class,'<br>';
      10. }


      同名空间成员的组织方式
      防止一个命名空间的代码过多,可以将同一个命名空间的代码写到多个脚本中

      1. namespace app;
      2. require 'namespce1.php';
      3. require 'namespace2.php';
      4. echo Demo2::class,'<br>';
      5. echo Model::class,'<br>';


      子命名空间
      ```html
      namespace Ns1{
      class Demo{
      }
      echo Demo::class,’<br>‘;
      // namespace用在空间中,标识当前空间的引用,类似$this或self
      // echo namespace\Ns2\Demo::class;
      echo Ns2\Demo::class,’<br>‘;
      echo Ns2\Ns3\Demo::class,’<br>‘;
      echo ‘<hr>‘;
      }
      namespace Ns1\Ns2{
      class Demo{

      }

}
namespace Ns1\Ns2\Ns3{
class Demo{

  1. }

}

  1. ![](https://img.php.cn/upload/image/333/968/852/1616144775828115.png)
  2. 子命名空间访问上级
  3. ```html
  4. echo Demo::class,'<br>';
  5. //从子级访问上级
  6. echo \Ns1\Ns2\Demo::class,'<br>';
  7. echo \Ns1\Demo::class,'<br>';
  8. //访问全局
  9. echo \Demo::class,'<br>';

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