复习控制访问权限

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

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

posted on 2012-12-07 22:02  php1994  阅读(170)  评论(0编辑  收藏  举报

导航