Reputation: 7712
I have the following code that I'm using in a small frame work I've put together. My framework didn't originally use Namespaces, but since two class names collide in this project I figured it would be a good time to try this out. The following error is thrown when the script is executed:
[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
Here is the following code snippets relevant to the error:
app/server.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');
}
}
This is the model/Minecraft.php file, which is what $game
resolves to.
<?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
);
}
}
If I remove the namespace from Minecraft class it works fine, but if I'm going to use namespaces shouldn't I go all the way?
Upvotes: 0
Views: 877
Reputation: 366
I found this question much sooner than I found a resolution to it. For everyone's benefit, here is a helpful link to resolving the issue at hand: Variable functions with namespaces in PHP
Upvotes: 1