【问题标题】:Getting an error trying to initialize this public class variable using dirname() outside a method尝试在方法外使用 dirname() 初始化此公共类变量时出错
【发布时间】:2011-05-27 17:54:09
【问题描述】:

为什么我不能使用函数设置公共成员变量?

<?

class TestClass {

    public $thisWorks = "something";

    public $currentDir = dirname( __FILE__ );

    public function TestClass()
    {
        print $this->thisWorks . "\n";
        print $this->currentDir . "\n";
    }

}

$myClass = new TestClass();

?>

运行它会产生:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in /tmp/tmp.php on line 7

【问题讨论】:

  • 变量声明中不能有表达式。您只能使用常量值。
  • @mario:这是一个答案,不是评论。
  • @delnan:只是我喜欢避免使用单行字作为答案;但现在制作了一些填充文本..

标签: php class


【解决方案1】:

变量声明中不能有表达式。您只能使用常量值。 dirname() 可能不会出现在此位置。

如果您要使用 PHP 5.3,您可以使用:

  public $currentDir = __DIR__ ;

否则你将不得不在__constructor 中初始化$this-&gt;currentDir

【讨论】:

    【解决方案2】:

    根据PHP manual,您的实例变量:

    必须能够在 编译时间,不能依赖 运行时信息,以便 评估

    因此,您不能在属性初始化中使用dirname 函数。因此,只需使用constructor 通过以下方式设置默认值:

    public function __construct() {
        $this->currentDir = dirname( __FILE__ );
    }
    

    【讨论】:

      【解决方案3】:

      指定属性时不能调用函数。

      改用这个:

      <?php
      
      class TestClass{
      
          public $currentDir = null;
      
          public function TestClass()
          {
              $this->currentDir = dirname(__FILE__);
              /* the rest here */
          }
      }
      

      【讨论】:

      • 这段代码毫无意义。公共全局变量?一个名为 TestClass 的公共全局函数?
      • 对不起,我忘了复制粘贴类定义
      • 他们已将构造函数名称更改为 __construct :)
      • 他们没有改变它,他们只是在某个时候添加了 __construct(PHP5?)。除非您使用 PHP5.3.3 或更高版本 (php.net/releases/5_3_3.php) 并且您的类位于命名空间内,否则作为构造函数的类的名称仍然有效。
      • 但他们正在远离该命名约定。
      【解决方案4】:

      在为成员变量指定默认值时,您似乎无法调用函数。

      【讨论】:

        【解决方案5】:

        很遗憾,您不能调用函数来声明类变量。但是,您可以在构造函数中将 dirname( FILE ) 的返回值分配给 $this->currentDir。

        编辑:请注意:PHP => 5 中的构造函数称为 __construct(),而不是类的名称。

        【讨论】:

          【解决方案6】:

          您可以改用这个:

          public $currentDir = '';
          
          public function TestClass()
          {
              $this->currentDir = dirname( __FILE__ );
              ...
          

          【讨论】:

            【解决方案7】:

            dirname 表达式导致您无法在此处将表达式声明为变量的错误。不过你可以这样做。

            <?
            
            class TestClass {
            
                public $thisWorks = "something";
                public $currentDir;
            
                public function __construct()
                {
                    $this->currentDir = dirname( __FILE__ );
                }
            
                public function test()
                {
                    echo $this->currentDir;
                }
            }
            

            每次实例化一个新类时,都会在构造函数中设置目录名。我还建议在您的文件中省略关闭 php 标记 ?> 。有助于缓解和标头发送错误

            【讨论】:

              【解决方案8】:

              原因是您不能以静态方式使用函数分配实例变量。 PHP 中根本不允许这样做。

              我建议你这样做:

              <?
              class Foo {
                  public $currentDir;
              
                  public function __construct() {
                      $this->currentDir = dirname(__FILE__);
                  }
              }
              ?>
              

              【讨论】:

              • 这种说法过于宽泛和错误(可能只是措辞粗心,但仍然如此)。
              【解决方案9】:

              在构造函数中执行。 $this->currentDir = 目录名(FILE);

              顺便说一句 打印 $currentDir 。 "\n"; 在类中调用 var 时使用 $this

              【讨论】:

                猜你喜欢
                • 2020-03-27
                • 1970-01-01
                • 1970-01-01
                • 2011-03-02
                • 1970-01-01
                • 2017-05-06
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多