我有一个这样的帮助类:
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'];
}
}
}
它给了我:
解析错误:语法错误,意外'('与公共静态$ app_url = self :: getServerUrl();
答案 0 :(得分:1)
您的问题是您正在尝试使用自静态函数定义静态变量。由于您从未实例化类(静态)并且您正在调用静态变量,因此无法调用自静态函数。
如果我复制粘贴您的代码并使用PHP 7运行它会产生其他错误:
致命错误:常量表达式在第4行的C:\ inetpub \ wwwroot \ test.php中包含无效操作
要解决您的问题,请使用:
<?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();