• 复习控制访问权限


    一个类的实例,在类外调用类的私有属性失败的例子:

    <?php
      class student
      {
          private $no;
          private $name;
          private $gender;
          private $age;
    
        private   function show_age(){
              echo"name is $this->name.<br />";
              }
          function ask_age(){
              $this->show_age();
              }
          
          function set_data($arr)
          {
              $this->no=$arr["no"];
              $this->name=$arr["name"];
              $this->gender=$arr["gender"];
              $this->age=$arr["age"];
              }
              
              function grow($i){
                  $this->age+=$i;
                  }
          function get_data(){
              echo"<br />
      <b>学生信息</b><br />
      ";
      echo "学号:$this->no<br />
      ";echo "姓名:$this->name<br />
      ";echo "性别:$this->gender<br />
      ";echo "年龄:$this->age<br />
      ";
              }
          }
      
    
     $s=new student;
          $temparr=array("no"=>"001","name"=>"lisa","gender"=>"male","age"=>"22");
     
      $s->set_data($temparr);
    
    echo "$s->age";
     
         
        ?>


    显示:Fatal error: Cannot access private property student::$age in
    C:\xampp\htdocs\v\v.php on line 55

    调用私有方法失败:

    <?php
      class student
      {
          private $no;
          private $name;
          private $gender;
          private $age;
    
        private   function show_age(){
              echo"name is $this->name.<br />";
              }
          function ask_age(){
              $this->show_age();
              }
          
          function set_data($arr)
          {
              $this->no=$arr["no"];
              $this->name=$arr["name"];
              $this->gender=$arr["gender"];
              $this->age=$arr["age"];
              }
              
              function grow($i){
                  $this->age+=$i;
                  }
          function get_data(){
              echo"<br />
      <b>学生信息</b><br />
      ";
      echo "学号:$this->no<br />
      ";echo "姓名:$this->name<br />
      ";echo "性别:$this->gender<br />
      ";echo "年龄:$this->age<br />
      ";
              }
          }
      
    
     $s=new student;
          $temparr=array("no"=>"001","name"=>"lisa","gender"=>"male","age"=>"22");
     
      $s->set_data($temparr);
    
    $s->show_age();
     
         
        ?>

    Fatal error: Call to private method student::show_age() from context ''
    in C:\xampp\htdocs\v\v.php on line 55

    通过调用公共方法,来实现调用私有方法:

    <?php
      class student
      {
          private $no;
          private $name;
          private $gender;
          private $age;
    
        private   function show_age(){
              echo"name is $this->name.<br />";
              }
          function ask_age(){
              $this->show_age();
              }
          
          function set_data($arr)
          {
              $this->no=$arr["no"];
              $this->name=$arr["name"];
              $this->gender=$arr["gender"];
              $this->age=$arr["age"];
              }
              
              function grow($i){
                  $this->age+=$i;
                  }
          function get_data(){
              echo"<br />
      <b>学生信息</b><br />
      ";
      echo "学号:$this->no<br />
      ";echo "姓名:$this->name<br />
      ";echo "性别:$this->gender<br />
      ";echo "年龄:$this->age<br />
      ";
              }
          }
      
    
     $s=new student;
          $temparr=array("no"=>"001","name"=>"lisa","gender"=>"male","age"=>"22");
     
      $s->set_data($temparr);
    
    $s->ask_age();
     
         
        ?>

    显示:name is lisa.

  • 相关阅读:
    lumen简单使用exel组件
    VIM 批量注释的两种方法 (转)
    linux时间校准 设置时间为上海时区
    lumen发送邮件配置
    centos 下安装redis 通过shell脚本
    shell 脚本接收参数
    linux设置系统变量
    linux通配符
    UCCI协议[转]
    一种编程范式:对拍编程
  • 原文地址:https://www.cnblogs.com/4php/p/2807943.html
Copyright © 2020-2023  润新知