<?php
    
  class person{     public $name;
      public $age;
      public $qq;      public $color;      function __construct($name,$age,$color)
     {
          $this->name=$name;
          $this->age=$age;
          $this->color=$color;
           
     }
      
     
  
      
    }  class student extends person
{      function __construct($name,$age,$color,$qq)
    {
        $this->name=$name;
        $this->qq=$qq;
    }
    
}  $sd=new student('xuxin',22,'bld',562332);   echo $td-name;
    $p=new person('ligen',22,'red');  echo $p->name,'<br />',$p->age,'<br />',$p->color;
?>构造函数怎么不能覆盖?

解决方案 »

  1.   

    <?php
        
      class person{     public $name;
          public $age;
          public $qq;      public $color;      function __construct($name,$age,$color)
         {
              $this->name=$name;
              $this->age=$age;
              $this->color=$color;
               
         }
          
         
      
          
        }  class student extends person
    {      function __construct($name,$age,$color,$qq)
        {
            $this->name=$name;
            $this->qq=$qq;
        }
        
    }  $sd=new student('xuxin',22,'bld',562332);   echo $td-name;
        $p=new person('ligen',22,'red');  echo $p->name,'<br />',$p->age,'<br />',$p->color;
    ?>
      

  2.   

    php不像java
    需要显示的调用父类的构造方法parent::__construct();
      

  3.   


    $sd=new student('xuxin',22,'bld',562332);
    echo $sd->name;------------------测试哥的签名--------------------------
    哥默默的低下头,不是哥修养好,而是哥在找砖头!
    --------------------------------------------------------
      

  4.   

    继承 extend 然后调用父类的构造方法parent:
      

  5.   


    class MyClass
    {
    public $public = 'Public1';
    protected $protectde = 'Protected1';
    private $private = 'Private1';
    function printHello()
    {
    echo $this->public."\n";
    echo $this->protected."\n";
    echo $this->private."\n";
    }
    }
    class MyClass2 extends MyClass
    {
    protected $protected = 'Protected2';
    function printHello()
    {
    echo $this->public."\n";
    echo $this->protected."\n";
    echo "--------\n";
    echo parent::printHello()."\n";
    }
    }$obj2 = new MyClass2();
    echo "<pre/>";$obj2->printHello();