Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php $this->;常量或$this->;类构造函数中的var_Php_Laravel_Class_Constructor_Constants - Fatal编程技术网

Php $this->;常量或$this->;类构造函数中的var

Php $this->;常量或$this->;类构造函数中的var,php,laravel,class,constructor,constants,Php,Laravel,Class,Constructor,Constants,我想知道下面的代码是否在结构上是错误的,如果不是,有人能向我解释为什么它实际工作吗 class ClassA extends ClassB { public function __construct() { $this->FILE_DIRECTORY = 'book/auido'; } public function index() { $file_directory = $this->FILE_DIRECTO

我想知道下面的代码是否在结构上是错误的,如果不是,有人能向我解释为什么它实际工作吗

class ClassA extends ClassB
{
    public function __construct()
    {
        $this->FILE_DIRECTORY = 'book/auido';
    }

    public function index()
    {
        $file_directory = $this->FILE_DIRECTORY;
        return $file_directory
    }
}
上述回报

'book/auido'
我知道正确的方法是
publicstatic$variable='string'
和使用
self::$variable
以在类中的任何方法中访问它


但是我觉得我上面使用的方法是错误的,因为我不完全理解发生了什么以及它为什么工作。

您如何使用
文件目录
这里不是一个常量,它只是一个大写的变量

以下是如何将其用作常数,以供参考:

ClassA扩展了ClassB
{
const FILE_DIRECTORY='book/auido';
公共职能指数()
{
$file\u directory=self::file\u directory;
返回$file\u目录
}
}

通常人们用常量来表示一些永远不会改变的东西,比如ISO国家代码。域中的任何内容都可能发生更改,因此此时配置通常会更好。

这一切都取决于您打算如何使用该类及其功能。也许静态变量有意义,也许没有。也许硬编码一个值是有意义的,也许它没有:)@lagbox这算是常数吗?不管是否在构造函数中传递。找到我的问题的答案并