PHP 中 解析错误的静态替代方案:语法错误, 中出现意外的 T_STATIC
我正在使用 Xamppp 在本地主机上测试我的 PHP 应用程序,一切都很好,但是当我将其导出到真实服务器时,它不再工作了。我发现这是因为我的服务器不支持后期静态绑定。 我的服务器版本为5.2.17。
我收到这个错误。
<b>Parse error</b>: syntax error, unexpected T_STATIC in <b>/home/storage/f/9d/09/meuplacar/public_html/filme/work/class_lib.php</b> on line <b>555</b><br />
我只是在我构建的 Util 类中使用 static
关键字。你对我改变这种方法有什么建议:
Util
class Util
{
private static $initialized = false;
private static function initialize()
{
if (self::$initialized)
return;
self::$initialized = true;
}
public static function getHoursAndMinutesFromTime($time) {
self::initialize();
$pieces = explode (":", $time);
$output = "";
$output = $pieces[0] . ":" . $pieces[1];
return $output;
}
}
对于一个独特的外观实例
Singleton
abstract class Singleton {
protected static $_instance = NULL;
/**
* Prevent direct object creation
*/
final private function __construct() {
}
/**
* Prevent object cloning
*/
final private function __clone() {
}
/**
* Returns new or existing Singleton instance
* @return Singleton
*/
final public static function getInstance(){
if(null !== static::$_instance){
return static::$_instance;
}
static::$_instance = new static();
return static::$_instance;
}
}
class Facade extends Singleton {
public function retrieveAllWorkdays()
{
$array = DB::selectAllWorkdays();
return Util::constructWorkdaysArray($array);
}
I was testing my PHP application on localhost with Xamppp and everything was just fine, but when I exported it to a real server, it does not work anymore. I found out that it is because my server does not support late static binding.
My server has version 5.2.17.
I get this error.
<b>Parse error</b>: syntax error, unexpected T_STATIC in <b>/home/storage/f/9d/09/meuplacar/public_html/filme/work/class_lib.php</b> on line <b>555</b><br />
I just use the static
keyword in a Util class I built. What would you suggest for me to change this kind of methods:
Util
class Util
{
private static $initialized = false;
private static function initialize()
{
if (self::$initialized)
return;
self::$initialized = true;
}
public static function getHoursAndMinutesFromTime($time) {
self::initialize();
$pieces = explode (":", $time);
$output = "";
$output = $pieces[0] . ":" . $pieces[1];
return $output;
}
}
And for a Unique Facade Instance
Singleton
abstract class Singleton {
protected static $_instance = NULL;
/**
* Prevent direct object creation
*/
final private function __construct() {
}
/**
* Prevent object cloning
*/
final private function __clone() {
}
/**
* Returns new or existing Singleton instance
* @return Singleton
*/
final public static function getInstance(){
if(null !== static::$_instance){
return static::$_instance;
}
static::$_instance = new static();
return static::$_instance;
}
}
class Facade extends Singleton {
public function retrieveAllWorkdays()
{
$array = DB::selectAllWorkdays();
return Util::constructWorkdaysArray($array);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
后期静态绑定仅在 php 5.3.0 及更高版本中可用: http ://php.net/manual/en/language.oop5.late-static-bindings.php
Late static bindings only are available in php 5.3.0 and later: http://php.net/manual/en/language.oop5.late-static-bindings.php