解析错误:语法错误,意外的'(',期待','或';'in

我收到以下解析错误:

解析错误:语法错误,在第7行的H: Programs USBWebserver v8.5 8.5 root oopforum func register.class.php中意外的'(',期待','或';'

它与我的课程中的以下代码行相关:

private $random_name = rand(1000,9999).rand(1000,9999).rand(1000,9999).rand(1000,9999);

我看不到为什么这行代码会导致解析错误?

这是一些周围的代码:

class register{
    public $post_data = array();
        private $dbh;
        private $allowed_type = array('image/jpeg','image/png','image/gif');
        private $random_name = rand(1000,9999).rand(1000,9999).rand(1000,9999).rand(1000,9999);
        private $path = 'img/thumb_/'.$random_name. $_FILES['file']['name'];
        private $max_width = 4040;
        private $max_height = 4040;
        private $max_size = 5242880;
        private $temp_dir = $_FILES['file']['tmp_name'];
        private $image_type = $_FILES['file']['type'];
        private $image_size = $_FILES['file']['size'];
        private $image_name = $_FILES['file']['name'];
        private $image_dimensions = getimagesize($temp_dir);
        private $image_width = $image_dimensions[0]; // Image width
        private $image_height = $image_dimensions[1]; // Image height
        private $error = array();

        public function __construct($post_data, PDO $dbh){
        $this->post_data = array_map('trim', $post_data);
        $this->dbh = $dbh;
        }
}

什么导致了分析错误?


您不能将成员变量初始化为任何不是静态的,并且您正试图调用一个函数。

从手册:

这个声明可能包括一个初始化,但是这个初始化必须是一个常量值 - 也就是说,它必须能够在编译时进行评估,并且不能依赖运行时信息来进行评估。

解决方法是在构造函数中设置变量:

private $random_name;
public function __construct() { 
    $this->random_name = rand(1000,9999).rand(1000,9999).rand(1000,9999).rand(1000,9999);
}

你不能这样做:

private $image_dimensions = getimagesize($temp_dir);

函数,变量和其他不是静态的东西不能被调用来设置类变量。

链接地址: http://www.djcxy.com/p/11985.html

上一篇: Parse error: syntax error, unexpected '(', expecting ',' or ';' in

下一篇: php unexpected ')' parse error