我有以下代码,我在我放在一起的一个小框架中使用。我的框架最初没有使用命名空间,但由于两个类名在这个项目中发生冲突,我认为现在是尝试这个的好时机。执行脚本时会抛出以下错误:
[Wed May 02 15:04:33 2012] [error] [client 127.0.0.1] PHP Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in /home/marco/Projects/stackgaming.com/app/server.php on line 17
以下是与错误相关的以下代码片段:
应用程序/服务器.php
if( !defined('IN_APP') ) { die('THESE ARE NOT THE DROIDS YOU ARE LOOKING FOR'); }
require_once('model/Server.php');
\Model\Server::$save_path = APPLICATION_ROOT . 'servers';
class Server extends App
{
public static function init($server_id)
{
$server_data = \Model\Server::get($server_id);
$game = $server_data['interface'];
require_once('model/' . $game . '.php');
$query_data = \Model\$game::query($server_data['host'], $server_data['query_port']);
var_dump($server_data);
var_dump($query_data);
//static::$View->display('user_main.tpl');
}
}
这是 model/Minecraft.php 文件,它被$game
解析为。
<?php
namespace Model;
class MinecraftException extends \Exception
{
// Exception thrown by Minecraft classes
}
class Minecraft
{
public static function query( $host, $port = 25565, $timeout = 3 )
{
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $timeout, 'usec' => 0));
if( $socket === false || @socket_connect($socket, $host, (int)$port) === false )
{
return false;
}
socket_send($socket, "\xFE", 1, 0);
$len = socket_recv($socket, $data, 256, 0);
socket_close($socket);
if( $len < 4 || $data[ 0 ] != "\xFF" )
{
return false;
}
$data = substr($data, 3);
$data = iconv('UTF-16BE', 'UTF-8', $data);
$data = explode("\xA7", $data);
return array
(
'hostname' => substr($data[0], 0, -1),
'total_players' => isset($data[1]) ? intval($data[1]) : 0,
'max_players' => isset($data[2]) ? intval($data[2]) : 0
);
}
}
如果我从 Minecraft 类中删除命名空间,它可以正常工作,但如果我要使用命名空间,我不应该一直走吗?