在PHP中连接lang CONST和STR CONST

在PHP中连接lang CONST和STR CONST,php,constants,concatenation,string-concatenation,Php,Constants,Concatenation,String Concatenation,正在尝试连接: <?php class MD_ImpFormularios extends CI_Model { private $dir_forms = __DIR__ . 'Hola'; 但我没有发现任何错误,它不是常量或静态变量,它是一个简单的变量 <?php class MD_ImpFormularios extends CI_Model { private $dir_forms = '';

正在尝试连接:

<?php
class MD_ImpFormularios extends CI_Model {
  private $dir_forms = __DIR__ . 'Hola';
但我没有发现任何错误,它不是常量或静态变量,它是一个简单的变量

    <?php
          class MD_ImpFormularios extends CI_Model {
                private $dir_forms = ''; 
                ....
                public function __construct(){
                      $this->dir_forms = __DIR__ . 'Hola'
                }

谢谢

声明类变量时不要进行连接

private $dir_forms = __DIR__ . 'Hola';
                          // ^ This is NOT allowed during declaration
private $dir_forms;
public function __construct() {
    $this -> dir_forms = __DIR__ . 'Hola';
}
您可以使用构造函数设置此类变量

private $dir_forms = __DIR__ . 'Hola';
                          // ^ This is NOT allowed during declaration
private $dir_forms;
public function __construct() {
    $this -> dir_forms = __DIR__ . 'Hola';
}

在声明类常量或变量时不能使用concatenation(直到php5.5,因为php5.6可以使用串联定义类属性,所以我发现在构造函数中进行此类操作更好)。 您应该声明为空字符串,并且可以在构造函数中为此变量赋值

    <?php
          class MD_ImpFormularios extends CI_Model {
                private $dir_forms = ''; 
                ....
                public function __construct(){
                      $this->dir_forms = __DIR__ . 'Hola'
                }

不能在类属性声明中执行此操作。您必须在构造函数中执行此操作:

<?php
class MD_ImpFormularios extends CI_Model {
  private $dir_forms;

  public function __construct() {
    $this->dir_forms = __DIR__ . 'Hola';
  }
}

这是因为您正在连接一个sting来设置一个属性,这是不允许的。您可以在中看到此示例:


您应该改为在构造函数中设置值。

hmmmmm我明白了,我正在查看文档。。。。。解析错误可以更好地告诉您出了什么问题,哈哈。@Jorge,它确实告诉您“出乎意料的