2012-07-03 51 views
16

我recieveing以下分析錯誤:解析錯誤:語法錯誤,意想不到的「(」,期望「」或‘;’在

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

其涉及下面的行的在我的類代碼:

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

什麼導致瞭解析錯誤?

回答

35

您不能將成員變量初始化爲任何非靜態的東西,並且您試圖調用一個函數。

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.

的解決方法是設置你的變量在構造函數:

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

+1打我吧 – Jacco

+0

謝謝..我刪除了一切,除了行設置$ random_name變量,並將其放置在__construct(),它給了我這個錯誤'解析錯誤:語法錯誤,意外的T_PRIVATE在H:\ Programs \ USBWebserver v8.5 \ 8.5 \ root \ oopforum \ func \ register.class.php on line 14' – crm

+0

你必須用完整的類更新你的問題來確定錯誤,因爲它可能與你的前一個。 – nickb

4

你不能做到這一點:

private $image_dimensions = getimagesize($temp_dir); 

函數,變量而其他不是靜態的不能被調用來設置類變量。

相關問題