2016-07-09 24 views
-1

は、私は、ヘルパー、このようなクラスがあります。解析エラー:構文エラー、予期しない「(」

class Helper{ 

    public static $app_url = self::getServerUrl(); 
    /** 
    * Gets server url path 
    */ 
    public static function getServerUrl(){ 
     global $cfg; // get variable cfg as global variable from config.php Modified by Gentle 

     $port = $_SERVER['SERVER_PORT']; 
     $http = "http"; 

     if($port == "80"){ 
      $port = ""; 
     } 

     if(!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on"){ 
      $http = "https"; 
     } 
     if(empty($port)){ 
      return $http."://".$_SERVER['SERVER_NAME']."/".$cfg['afn']; 
     }else{ 
      return $http."://".$_SERVER['SERVER_NAME'].":".$port."/".$cfg['afn']; 
     }   
    } 
} 

とをその与えられた私:

Parse error: syntax error, unexpected '(' on the line with public static $app_url = self::getServerUrl();

答えて

1

あなたの問題は、ユーザーが定義しようとしているということですクラスをインスタンス化したことがなく、静的変数を呼び出しているので、自己静的関数を呼び出すことはできません。

コードをコピーしてPHPで実行するとこれを使用し、あなたの問題を解決するために

Fatal error: Constant expression contains invalid operations in C:\inetpub\wwwroot\test.php on line 4

:7それは他のエラーを与え、それが働いた

<?php 
class Helper { 

    public static $app_url; 

    public static function Init() { 
     self::$app_url = self::getServerUrl(); 
    } 

    public static function getServerUrl(){ 

     global $cfg; // get variable cfg as global variable from config.php Modified by Gentle 

     $port = $_SERVER['SERVER_PORT']; 
     $http = "http"; 

     if($port == "80"){ 
      $port = ""; 
     } 

     if(!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on"){ 
      $http = "https"; 
     } 
     if(empty($port)){ 
      return $http."://".$_SERVER['SERVER_NAME']."/".$cfg['afn']; 
     }else{ 
      return $http."://".$_SERVER['SERVER_NAME'].":".$port."/".$cfg['afn']; 
     } 

    } 

} 
Helper::Init(); 
+0

おかげで、私は、私は定数などを使用したい場合は、お聞きしたい:P0lT10nそれ@ – gentle

+0

感謝を私は定数を使用したいかどうか質問したい:const APP_URLl; public static function Init(){ セル:: APP_URL =セル:: getServerUrl(); }エラーを返す – gentle

+0

定数を宣言しているので、エラーが出ます。それを定数として宣言する方法はありません。私の答えを正しいものとしてマークしてください – matiaslauriti

関連する問題