我有一个Bicycle类,其中常量变量protected const CONDITION_OPTIONS
,每当我尝试回显来自condition()
方法的返回值时,此消息就会出现
Fatal error: Uncaught Error: Undefined class constant 'CONDITION_OPTIONS' in C:\Users\Khaled\Desktop\sites\chain_gang\private\classes\bicycle.class.php:64 Stack trace: #0 C:\Users\Khaled\Desktop\sites\chain_gang\public\bicycles.php(38): Bicycle->condition() #1 {main} thrown in C:\Users\Khaled\Desktop\sites\chain_gang\private\classes\bicycle.class.php on line 64
请检查下面的代码...
<?php
class Bicycle {
protected $condition_id;
protected const CONDITION_OPTIONS = [
1 => 'Beat up',
2 => 'Decent',
3 => 'Good',
4 => 'Great',
5 => 'Like New'
];
public function __construct($arg=[]){
$this->condition_id = $arg['condition_id'] ?? 3;
}
public function condition(){
if($this->condition_id > 0){
return self::CONDITION_OPTIONS[$this->condition_id];
}else{
return "Unknown";
}
}
}
$arg = ['condition_id' => 2];
$bike = new Bicycle($arg);
echo $bike->condition();
?>