2012-07-03 14 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()の中に配置しました。このエラーは 'Parse error:syntax error、unexpected T_PRIVATE in H:\ Programs \ USBWebserver v8.5 \ 8.5 \ root \ oopforum \ func \ register.class.php 14行目 – crm

+0

あなたの質問をフルクラスで更新して、そのエラーを無関係であると判断する必要がありますあなたの以前のもの。 – nickb

4

あなたがこれを行うことはできません。

private $image_dimensions = getimagesize($temp_dir); 

関数、変数、 static以外のものはクラス変数を設定するために呼び出すことはできません。

関連する問題