Reputation: 13
When I open 2 different sockets in php, I first get different resource id, but after open next socket, get equal resource id. Below is the result of the connection to three different sockets
# ./test.php
127.0.0.1:26379/132458106d92e8f7b 127.0.0.1:26379 - Resource id #8 127.0.0.1:6380/320458106d92e906e 127.0.0.1:6380 - Resource id #9 127.0.0.1:6381/102858106d92e9106 127.0.0.1:6381 - Resource id #10 Resource id #10Array ( [timed_out] => [blocked] => 1 [eof] => [stream_type] => tcp_socket/ssl [mode] => r+ [unread_bytes] => 0 [seekable] => )
Resource id #10Array ( [timed_out] => [blocked] => 1 [eof] => [stream_type] => tcp_socket/ssl [mode] => r+ [unread_bytes] => 0 [seekable] => )
Resource id #10Array ( [timed_out] => [blocked] => 1 [eof] => [stream_type] => tcp_socket/ssl [mode] => r+ [unread_bytes] => 0 [seekable] => )
I'm use below code:
class RedisClient
function __construct ($arr = array ())
{
if (count($arr) === 0) return FALSE;
self::$host = $arr[0];
self::$port = $arr[1];
self::$persistent = '/' . uniqid(rand(100,10000));
self::$fp =
stream_socket_client('tcp://'.self::$host . ':' .
self::$port .
self::$persistent, $errno, $errstr, self::$connect_timeout, self::$flags);
echo self::$host,':',self::$port,self::$persistent,PHP_EOL;
if (!self::$fp) {
echo "$errstr ($errno)" . PHP_EOL;
return FALSE;
}
echo self::$host,':',self::$port,' - ';
print_r(self::$fp);
echo PHP_EOL;
if (!stream_set_timeout(self::$fp, self::$timeout)) return FALSE;
}
function getMeta ()
{
print_r (self::$fp);
print_r (stream_get_meta_data(self::$fp));
return;
}
$c=new RedisClient(array('127.0.0.1',26379));
$m=new RedisClient(array('127.0.0.1',6380));
$s=new RedisClient(array('127.0.0.1',6381));
$c->getMeta();
echo PHP_EOL;
$m->getMeta();
echo PHP_EOL;
$s->getMeta();
echo PHP_EOL;
exit;
Anybody know, why after all sockets connected, all Resource id, are indentical? And how make it different ?
Upvotes: 1
Views: 193
Reputation: 1687
You used self::$fp (static variable), you need use $this in class to get different variable by object.
"self::$var = 1;" - create one variable to class (without object)
"$this->var = 1;" - create attribute in object
For example you can write:
class Test {
protected $a = 1;
protected static $b = 1;
public function incA()
{
$this->a++;
}
public static function incB()
{
self::$b++;
}
public function getA()
{
return $this->a;
}
public static function getB()
{
return self::$b;
}
}
$test = new Test();
$test->incA();
$test->incA();
var_dump($test->getA());
var_dump(Test::getB()); // or $test::getB();
Test::incB();
$test2 = new Test();
var_dump($test2->getA());
var_dump($test2::getB());
var_dump($test::getB());
var_dump(Test::getB());
Upvotes: 1