PHP类设计问题

时间:2010-07-27 00:30:13

标签: php class oop

我正在尝试编写课程,以便在我的技能测试中向某些人展示。其中一个问题如下:

显示孩子如何调用父方法。显示重载是如何工作的。指出类变量的语法。

我认为我已经完成了大部分问题,但不确定表明类变量的语法是什么意思 ...有人能为我解释一下吗?非常感谢!

Class Phone{

 function __construct(){
     echo "This is the constructor";
 }

 protected function feature(){
  echo "Communication <br>";
 }
 }

 Class CellPhone extends CordlessPhone {
   private $brand;
   function __construct(){
  echo "<p style='color:red;'>Cell phone feature:</p>";
   }

   function __set($b, $value){
    if($value=="Apple"){
    $this->brand=$value." is awesome!";
    }elseif($value=="LG"){
    $this->brand=$value." is nice";
    }else{
    $this->brand="We only accept Apple and LG";
    }
   }

   function __get($brand){
    return "The brand: ".$this->brand."------";
    }

   public function feature(){
   parent::feature();
   echo "Play music<br>";
   echo "Surf internet<br>";
   }
 }

 Class CordlessPhone extends Phone {
   function __construt(){
  echo "<p style='color:red;'>Cordless phone feature:</p>";
   }
   public function feature(){
   parent::feature();
   echo "Cordless<br>";
   echo "Use Battery<br>";
   }
 }

 $phone=new CellPhone();
 $phone->feature();
 $phone->brand="LG";
 echo $phone->brand;

1 个答案:

答案 0 :(得分:3)

我认为它们将类变量表示为该类的属性。

class A {
   private $b = TRUE; // This is the syntax for setting visibility, name and default value

   public function __construct() {
       echo $this->b; // this is how to access from within a method
   }

}