1

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 ?

1
  • I think using static variable in non-static context would yield a warning or notice. You shouldn't mix contexts this way but also declare your variables in the class. Commented Oct 26, 2016 at 9:58

1 Answer 1

1

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

Read more here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.