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

发布于 2024-10-08 17:54:16 字数 1408 浏览 6 评论 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

I had this:

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

}

But I would like to add a timeout option to this class so I've added:

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

}

I'm getting an error saying: "Only variables can passed by reference."

What's going on?

Update:
The error: "Only variables can be passed by reference" is related to this line:

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

Thanks a lot,
MEM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2024-10-15 17:54:16
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)

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

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

The &$errno and &$errstr parameters are passed by reference. You can not use an empty string '' as argument there, since this is not a variable that can be passed by reference.

Pass a variable name for these parameters, even if you're not interested in them (which you should be, though):

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

Be careful to not overwrite existing variables with the same name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文