• 复习控制访问权限


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

    <?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.

  • 相关阅读:
    storm原理写得比较好的文章
    maven设置jdk版本
    项目中记录log4j记录日志
    eclipse jadeclipse配置
    Maven使用说明
    crond不执行原因分析
    空调遥控器图标含义
    window7开放端sqlserver端口
    servlet仿struts参数注入
    cocos 2dx-js3.5整合anySDK
  • 原文地址:https://www.cnblogs.com/4php/p/2807943.html
Copyright © 2020-2023  润新知