复习继承已有的类

类的继承的例子:

 1 <?php
 2  class student
 3  {
 4      var $no;
 5      var $name;
 6      var $gender;
 7      var $age;
 8      
 9      function set_data($arr)
10      {
11          $this->no=$arr["no"];
12          $this->name=$arr["name"];
13          $this->gender=$arr["gender"];
14          $this->age=$arr["age"];
15          }
16          
17          function grow($i){
18              $this->age+=$i;
19              }
20      function get_data(){
21          echo"<br />
22  <b>学生信息</b><br />
23  ";
24  echo "学号:$this->no<br />
25  ";echo "姓名:$this->name<br />
26  ";echo "性别:$this->gender<br />
27  ";echo "年龄:$this->age<br />
28  ";
29          }
30      }
31  
32 class college_student extends student{
33     var $department;
34 
35     function change_department($new_department){
36         $this->department=$new_department;
37         }
38     } 
39 $s=new college_student;
40      $temparr=array("no"=>"001","name"=>"lisa","gender"=>"male","age"=>"22");
41 
42  $s->set_data($temparr);
43  $s->get_data();
44  $s->department="物理系";
45  echo "$s->name"."的所在系是$s->department<br />";
46 $s->change_department("数学系");
47   echo "$s->name"."的所在系是$s->department<br />";
48     
49     
50    ?>

子类 college_student 继承了它的父类student的全部属性和方法,并且子类在第33行又声明了一个自己的新属性。
39行,实例化一个子类的对象,在42行、43行,这个子类对象调用了父类的两个方法,在地45行和47行里,子类对象调用了父类的属性以及它自己的新属性。

运行显示:

学生信息
学号:001
姓名:lisa
性别:male
年龄:22
lisa的所在系是物理系
lisa的所在系是数学系

在编写代码的过程中,遇到一个问题,就是,如果将第47行的代码写成:

  echo "$s->name的所在系是$s->department<br />";

那么就运行时,就会显示name属性未定义。这其实是与name属性没有关系的,而是echo的写法原因造成的,
以下三种写法,程序会正常显示出来:

(1) echo $s->name."的所在系是$s->department<br />";

(2)echo "$s->name"."的所在系是$s->department<br />";

(3)echo “所在系是$s->department<br />";

这说明。如果echo 输出的第一个字符是一个变量的时候,要么用连字符”."把这个变量与后面的字符串分隔开,要么就把这个变量用""括起来并用连字符与后面的字符串相连接。如果echo输出的字符串中,变量所在的位子不在第一个字符,那么就无需使用连字符分隔。

为了验证这个说法,我写了这样一段代码:

 1 <?php
 2 
 3 $foo = "foo";
 4 $bar = "bar";
 5 
 6 echo "Foo is $foo<br />";
 7 echo "$bar is bar<br />";
 8 echo "$bar"." is bar<br />";
 9 echo $bar." is bar<br />";
10 echo "bar is $bar<br />";
11    ?>

那么这个程序,按照上面的说法,应该显示:

Foo is foo
错误提示,提示$bar未定义
bar is bar
bar is bar
bar is bar

可是实际,运行的结果却是:

Foo is foo
bar is bar
bar is bar
bar is bar
bar is bar

第二行可以正常运行出来,为了测试第二行,我把其它代码删除。

<?php

$bar = "bar";
echo "$bar is bar<br />";

   ?>

运行显示:

bar is bar

这说明,之前的猜测不对,这就奇怪了,为什么 echo "$s->name的所在系<br />";不能显示,而echo "$bar is bar<br />"; 却可以显示呢?

我将$bar变量使用与$s->name变量相同的调用方法重新写一遍程序。

将:

<?php

$bar = "bar";
echo "$bar is bar<br />";

   ?>

这段程序写为:

 1 <?php
 2 
 3 class test{
 4     var $bar;
 5     function give_content($con){
 6         $this->bar=$con;
 7                 }
 8     }
 9 
10 $s=new test;
11 $s->give_content("bar");
12 echo "$s->bar is bar<br />";
13 
14    ?>

看看这段程序运行后是什么效果?结果运行后依然正常:bar is bar

这就说明,之前关于echo的猜测都不对,与echo输出的第一个字符没有关系,与对象调用的属性变量也没有关系。那么,难道问题出在类的继承上吗?

下面我再写一个类的继承的程序,再来看一下运行后,是否有错误提示:

<?php

class test{
    var $bar;
    function give_content($con){
        $this->bar=$con;
                }
    }


class new_test extends test{}// new_test 继承test类,继承后,并不给予new_test 新的属性和新的方法
$s=new new_test;
$s->give_content("bar"); //调用了父类的方法
echo "$s->bar is bar<br />"; //调用了父类的$bar属性

   ?>

结果依然正常,运行显示:bar is bar,这说明问题和继承也没有关系。

那么再回去看一下原来的程序,我将不相关的程序删除,程序可简化为:

 1 <?php
 2   class student
 3   {
 4       var $name;
 5       function set_data($arr)
 6       {
 7 
 8           $this->name=$arr["name"];
 9 
10           }
11           
12 
13       }
14   
15  class college_student extends student{} 
16  $s=new college_student;
17       $temparr=array("name"=>"lisa");
18  
19   $s->set_data($temparr);
20 
21    echo "$s->name的所在系<br />";
22      
23      
24     ?>

运行结果为:
Notice: Undefined property: college_student::$name的所在系 in
C:\xampp\htdocs\v\v.php on line 31

这个有可能是数组造成的,如果不用数组赋值,程序为:

 1 <?php
 2   class student
 3   {      var $name;      }
 4   
 5  class college_student extends student{} 
 6  $s=new college_student;
 7 $s->name="lisa";
 8    echo "$s->name的所在系<br />";
 9      
10      
11     ?>

问题依然存在,这说明不是数组的问题。

上面代码第8行,如果在name后面加一个空格,那么结果可以正常运行,这说明,变量的调用后面不能直接接其它字符串。验证一下:

<?php

$bar = "bar";
echo "$baris bar<br />"; //错误
echo "$bar is$bar<br />"; //正常显示
echo "$bar is $bar是错误。<br />"; //错误,
   ?>

显示:

Notice: Undefined variable: baris in C:\xampp\htdocs\v\v.php on
line 14
bar
bar isbar

Notice: Undefined variable:
bar是错误。 in C:\xampp\htdocs\v\v.php on line 16
bar is

这说明,变量后面不能直接加字符串,否则会被php认为变量与字符串加在一起后,是另外一个变量。

原文地址:https://www.cnblogs.com/4php/p/2798912.html