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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11313051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:15:14  来源:igfitidea点击:

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

phpsyntax-error

提问by crmepham

I am recieveing the following parse error:

我收到以下解析错误:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in H:\Programs\USBWebserver v8.5\8.5\root\oopforum\func\register.class.php on line 7

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

which relates to the following line of code in my class:

这与我班级中的以下代码行有关:

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

I can not see why this line of code would cause a parse error?

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

Here is some surrounding code:

这是一些周围的代码:

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;
        }
}

What is causing the parse error?

导致解析错误的原因是什么?

回答by nickb

You can't initialize member variables to anything that is not static, and you're trying to call a function.

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

From the manual:

手册

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

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

The workaround is to set your variable in the constructor:

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

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

回答by Tyler Carter

You can't do this:

你不能这样做:

private $image_dimensions = getimagesize($temp_dir);

Functions, variables, and anything else not static can't be called to set class variables.

不能调用函数、变量和其他任何非静态的东西来设置类变量。