Php 未定义的类常数';条件选项';

Php 未定义的类常数';条件选项';,php,Php,我有一个类Bicycle,其中常量变量protected const const CONDITION\u OPTIONS,每当我尝试回显来自CONDITION()方法的返回值时,就会出现此消息 Fatal error: Uncaught Error: Undefined class constant 'CONDITION_OPTIONS' in C:\Users\Khaled\Desktop\sites\chain_gang\private\classes\bicycle.class.php:6

我有一个类Bicycle,其中常量变量
protected const const CONDITION\u 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();
?>


您的示例对我来说很好:可能您的实际代码有一个输入错误,您在这里发布答案时意外地修复了它?在PHP7.1中对我来说很好-返回
delegate
。您确定使用的是php>=7.1吗?@aynber常量只能与静态访问器一起使用
$this->FOO
将是属性
$FOO
$this::FOO
获取类($this)::FOO
@IMSoP-ah,谢谢。我以前从未在同一个类中使用过类常量。是的,我这么认为是因为我复制了代码并在我的项目中通过了它,现在它工作得很好xD感谢dude:D@IMSoP