Instead of this:
function chat($phrase) {
$p->_send_packet(array('s', 'm#sm', $room, $p->player['id'], $phrase, ''));
}
Try this:
function chat($phrase, $p) {
$p->_send_packet(array('s', 'm#sm', $room, $p->player['id'], $phrase, ''));
}
Then wherever you call the "chat()" function, add the $p as the second parameter:
chat('some phrase', $p);