2

So I've been trying to get an IRC bot working with PHP, to run locally. However, I want it to be able to retrieve information from a site, and post this information periodically. So I installed pthreads to do that. However, I'm having a bit of trouble with Referencing variables.

This is the error I get, when attempting to connect the bot to the channel.

Fatal error: Cannot assign by reference to overloaded object in C:\Apache24\htdo cs\muhbot.php on line 128

Here is the code that it's giving me an error on. Line 128 is the last line of the __construct() function.

class recheck extends Thread {
    var $lastCheck,$bot;
    public function __construct(&$bot){
        $this->lastCheck = microtime(true);
        $this->bot = &$bot;
    }

$bot is the class object that manages the IRC connection. The class is created by a function inside that class.

function startCheck()
{
    $ReChecker = new recheck($this);
    $ReChecker->start();
}

I'm trying to pass the main class as a reference, but keep getting the fatal error from above. Why am I getting this error, and how do I fix/work around it?

TUSF
  • 121
  • 2

1 Answers1

0

The $this that you pass to the recheck constructor is a reference to the current object and is passed by reference (Are PHP5 objects passed by reference?).

Get rid of all your & an it should work as expected

   public function __construct($bot){
    $this->lastCheck = microtime(true);
    $this->bot = $bot;
  }
Community
  • 1
  • 1
Ray
  • 40,256
  • 21
  • 101
  • 138
  • This was actually what I tried first, before calling it as a reference. But I get another error later, when I try to access the connection. "PHP Warning: fputs() expects parameter 1 to be resource, integer given in C:\Apache24\htdocs\muhbot.php on line 92". For some reason it's reading class property as an integer, rather than a resource. – TUSF Sep 18 '14 at 22:28
  • The function `fputs()` writes to a resource handle (like a file) which it expects as it's first argument, not an object. Your `fputs` error has nothing to do with object references--you shouldn't be passing it an object. – Ray Sep 19 '14 at 13:54