【问题标题】:Only variable can pass by reference - opensocket issue只有变量可以通过引用传递 - opensocket 问题
【发布时间】:2011-05-27 02:01:49
【问题描述】:

我有这个:

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);

非常感谢, 内存

【问题讨论】:

  • (叹气)在哪一行你得到了错误?
  • @Pekka - 谢谢。我已经更新了我的问题。 Ps-我需要那些'',''吗?

标签: php variables pass-by-reference fsockopen


【解决方案1】:
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)

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

【讨论】:

    猜你喜欢
    • 2011-02-27
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 2021-07-11
    相关资源
    最近更新 更多