php open 2 tcp socket -
when open 2 different sockets in php, first different resource id, after open next socket, equal resource id. below result of connection 3 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 sockets connected, resource id, indentical? , how make different ?
you used self::$fp (static variable), need use $this in class different variable object.
"self::$var = 1;" - create 1 variable class (without object)
"$this->var = 1;" - create attribute in object
for example 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());
Comments
Post a Comment