errno = $errno; $this->text = $text; } public function __toString() { return sprintf("%s: (%s, %s)", get_class($this), $this->errno, $this->text); } } class Socket { private $sock; public function __construct($domain=AF_INET, $type=SOCK_STREAM, $proto=SOL_TCP) { $this->sock = socket_create($domain, $type, $proto); if($this->sock === null) { throw new SocketError(); } } public function __call($method, $args) { $func = sprintf('socket_%s', $method); if(function_exists($func)) { array_unshift($args, $this->sock); $ret = call_user_func_array($func, $args); if($ret === false) { throw new SocketError(); } else { return $ret; } } else { throw new Exception(sprintf("method %s::%s does not exist", get_class($this), $method)); } } public function getsockname() { $host = ""; $port = -1; socket_getsockname($this->sock, &$host, &$port); return array($host, $port); } public function getpeername() { $host = ""; $port = -1; socket_getpeername($this->sock, &$host, &$port); return array($host, $port); } }