php - 在 php 中定义常量

标签 php class-constants

如何在函数中定义常量

例如。

class {

     public test;

     function tester{

      const test = "abc";

     }

  }

最佳答案

你做得很好,但你需要将 const 放在类级别而不是函数内部,例如:

class {
 const TEST = "abc"; 
 public $test2;

 function tester{
  // code here
 }
}

More info here.

此外,您在公共(public)变量 test 中缺少 $

关于php - 在 php 中定义常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2756069/

相关文章:

javascript - PHP/AJAX : Ajax request to php not working, 未将行放入 div

C# 构造函数和常量之谜

Swift init 无限循环,默认常量值

c++ - 直接从另一个类实例化时成员整数发生变化

excel - 为 Excel VBA 创建常量库的理想方法是什么?

php - 检查提交时表单值是否已更改

php - 有没有办法遍历一组已发布的变量,检查它们是否已设置?

Php artisan 无法迁移

php - 以 30 分钟为单位显示上午 9 点至下午 5 点 + 数组以删除 'unavailable' 次

php 类常量可见性