0

我有这个:

final public function __construct()
{
  $this->_host = 'ssl://myserver.com';
  $this->_porto = 700;
  $this->_filePointer = false;

  try
  {
    $this->_filePointer = fsockopen($this->_host, $this->_porto);
    if ($this->_filePointer === FALSE)
    {
       throw new Exception('Cannot place filepointer on socket.');
    }
    else
    {
       return $this->_filePointer;
    }

 }

 catch(Exception $e)
 {
            echo "Connection error: " .$e->getMessage();
 }

}

但我想为这个类添加一个超时选项,所以我添加了:

final public function __construct()
{
  $this->_host = 'ssl://myserver.com';
  $this->_porto = 700;
  $this->_filePointer = false;
  $this->_timeout = 10;

  try
  {
    $this->_filePointer = fsockopen($this->_host, $this->_porto, '', '', $this->_timeout);
    if ($this->_filePointer === FALSE)
    {
       throw new Exception('Cannot place filepointer on socket.');
    }
    else
    {
       return $this->_filePointer;
    }

 }

 catch(Exception $e)
 {
            echo "Connection error: " .$e->getMessage();
 }

}

我收到一条错误消息:“只有变量可以通过引用传递。”

这是怎么回事?

更新: 错误:“只能通过引用传递变量”与此行有关:

$this->_filePointer = fsockopen($this->_host, $this->_porto, '', '', $this->_timeout);

非常感谢,MEM

4

1 回答 1

3
fsockopen ( string $hostname [, int $port = -1 [, int &$errno [,
            string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )

和参数通过引用传递&$errno&$errstr您不能''在那里使用空字符串作为参数,因为这不是可以通过引用传递的变量。

为这些参数传递一个变量名,即使你对它们不感兴趣(不过你应该对它们感兴趣):

fsockopen($this->_host, $this->_porto, $errno, $errstr, $this->_timeout)

注意不要覆盖同名的现有变量。

于 2010-12-17T11:43:18.480 回答