Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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?

like image 596
crmepham Avatar asked Jul 03 '12 14:07

crmepham


People also ask

What is parse error syntax error?

If the PHP code contains a syntax error, the PHP parser cannot interpret the code and stops working. For example, a syntax error can be a forgotten quotation mark, a missing semicolon at the end of a line, missing parenthesis, or extra characters.

What is syntax error unexpected?

Syntax Error – This error is caused by an error in the PHP structure when a character is missing or added that shouldn't be there. Unexpected – This means the code is missing a character and PHP reaches the end of the file without finding what it's looking for.


2 Answers

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);
}
like image 69
nickb Avatar answered Sep 21 '22 21:09

nickb


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.

like image 20
Tyler Carter Avatar answered Sep 20 '22 21:09

Tyler Carter