只有变量可以通过引用传递 - opensocket问题

时间:2010-12-17 11:34:49

标签: php variables pass-by-reference fsockopen

我有这个:

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

1 个答案:

答案 0 :(得分: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)

小心不要覆盖具有相同名称的现有变量。