PHP类设计问题

标签 php class oop

我正在尝试编写类(class)来向一些人展示我的技能测试。其中一个问题如下:

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

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

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;

最佳答案

我认为它们的意思是类变量作为该类的属性。

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
   }

}

关于PHP类设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3339942/

相关文章:

oop - 如何处理交互装饰器

php - 在页面上包含 API 的最有效方法是什么?

java - 在静态内部类中使用静态变量

javascript - 在 Html 元素名称中获取双引号

php - MySQL:一次查询多行更新

C++构造函数默认值头文件

python - 类中的条件语句,但在函数范围之外

Java 泛型未经检查的覆盖

php - 是否可以知道 php 是否为 ://input contains data before starting reading it?

javascript - 如何内联IMG SRC数据:image Be Transferred With JavaScript to PHP?