duancheng1955
duancheng1955
2011-05-27 17:54
浏览 43
已采纳

尝试使用方法外部的dirname()初始化此公共类变量时出错

Why can't I set a public member variable using a function?

<?

class TestClass {

    public $thisWorks = "something";

    public $currentDir = dirname( __FILE__ );

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

}

$myClass = new TestClass();

?>

Running it yields:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in /tmp/tmp.php on line 7
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

9条回答 默认 最新

  • dongxun2089
    dongxun2089 2011-05-27 17:57
    已采纳

    You cannot have expressions in the variable declarations. You can only use constant values. The dirname() may not appear in this position.

    If you were to use PHP 5.3 you could use:

      public $currentDir = __DIR__ ;
    

    Otherwise you will have to initialize $this->currentDir in the __constructor.

    点赞 评论
  • dongli2000
    dongli2000 2011-05-27 17:56

    Looks like you can not call functions when specifying default values for member variables.

    点赞 评论
  • dousi2013
    dousi2013 2011-05-27 17:56

    The reason is that you cannot assign instance variables using functions in a static manner. It is simply not allowed in PHP.

    May I suggest you do this:

    <?
    class Foo {
        public $currentDir;
    
        public function __construct() {
            $this->currentDir = dirname(__FILE__);
        }
    }
    ?>
    
    点赞 评论
  • duanran3115
    duanran3115 2011-05-27 17:56

    You can't call functions to declare class variables, sadly. You could, however, assign the return value from dirname( FILE ) to $this->currentDir from within the constructor.

    EDIT: Mind you: the constructor in PHP => 5 is called __construct( ), not the name of the class.

    点赞 评论
  • doutu1889
    doutu1889 2011-05-27 17:57

    You cannot call functions when you specify attributes.

    Use this instead:

    <?php
    
    class TestClass{
    
        public $currentDir = null;
    
        public function TestClass()
        {
            $this->currentDir = dirname(__FILE__);
            /* the rest here */
        }
    }
    
    点赞 评论
  • drouie2014
    drouie2014 2011-05-27 17:57

    do it in the constructor. $this->currentDir = dirname( FILE );

    and by the way print $currentDir . " "; use $this when calling vars in class

    点赞 评论
  • dongyuan8469
    dongyuan8469 2011-05-27 17:58

    You can use this instead:

    public $currentDir = '';
    
    public function TestClass()
    {
        $this->currentDir = dirname( __FILE__ );
        ...
    
    点赞 评论
  • drwghu6386
    drwghu6386 2011-05-27 17:58

    As per the PHP manual, your instance variables:

    must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated

    As such, you can't use the dirname function in the property initialisation. Therefore, simply use a constructor to set the default value via:

    public function __construct() {
        $this->currentDir = dirname( __FILE__ );
    }
    
    点赞 评论
  • doulierong0334
    doulierong0334 2011-05-27 17:59

    The dirname expression is causing the error you can not declare an expression as a variable there. You could do this though.

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

    Everytime you instantiate a new class the dirname will be set in the constructor. I also recommend omitting the closing php tag ?> in your files. Helps to alleviate and header sent errors

    点赞 评论

相关推荐