2010-06-21 3 views
0

데이터베이스에서 값을 가져 와서 반환하는 함수가 있습니다. 나는 멤버 변수로 저장하는 함수를 호출하지만 다음과 같은 오류 얻을 :이 오류를php classe : 함수에서 멤버 변수로 값을 가져 오는 중 문제가 발생했습니다.

public static $depositmoney = self::get_balance(); 

의 원인이 라인은

Parse error: parse error, expecting `','' or `';'' in I:\wamp\www\deposit\classes\Site.php on line 14 

그리고 이것은에서 값을 가져옵니다 기능입니다 데이터베이스

public static function get_balance() 
    { 
     global $link, $usertable, $userid, $useridentify; 

     //query current balance 
     $cb = mysqli_fetch_object(mysqli_query($link, "SELECT deposit FROM ".$usertable." WHERE ".$userid."=".$useridentify."")); 
     return $cb->deposit; 

    }//end of function get_balance(). 

이 코드의 내용은 모두 같은 클래스입니다. 누구든지 오류의 원인에 대해 알고 있습니까?

답변

3

클래스 속성은 런타임 정보와 함께 선언되지 않을 수 있습니다.

public static $depositmoney = self::get_balance(); 

위의 사항은 작동하지 않습니다.

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. 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.

당신은 $depositmoney에 대한 게터를 생성하고 현재 설정되지 않은 경우이 값을 초기화 가질 수있는 PHP Manual on Class Properties: (강조 광산)를 참조하십시오 : 그러나

public static function getDepositMoney() 
{ 
    if(self::$depositmoney === NULL) { 
     self::$depositmoney = self::get_balance(); 
    } 
    return self::$depositmoney; 
} 

, 내가 얻을 제안 없애고 static을 제거하고 대신 상태를 추적하기 위해 인스턴스 메서드와 속성을 사용하십시오. 또한 global 항목을 제거하고 생성자, 설정자 또는 메소드 호출을 통해 종속성을 주입하려고합니다. 이는 커플 링을 줄이고 코드를보다 관리하기 쉽게 만듭니다.