2016-02-13 12:46
浏览 48


I have a associative array type property in my object. Here's the example:

class GetInfo {    

    private $domains_ip = array();

    function get_ip($domain)

        $ip = gethostbyname($domain);       
        $this->domains_ip[$ip] = $domain;       
        return $ip;      



class my_thread extends Thread {

    private $get_info_object;

    function __construct(GetInfo $obj)
        $this->get_info_object = $obj;

    function check_ip($domain)
        echo $this->get_info_object->get_ip($domain);

$o = new GetInfo();
$t = new my_thread($o);


But problem is that $this->domains_ip value doesn't change. What appropriate construction should I use in order to add value in this type of property. It works fine without passing it to thread object, but I it needed for my task. Thank you.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpkpaxhzffixp8426
    dpkpaxhzffixp8426 2016-02-13 13:15

    Since GetInfo does not descend from pthreads (it is not Threaded):

    $this->get_info_object = $obj;

    This results in a serial representation of $obj being stored as a member of the Thread. This results in the members of GetInfo being serialized and will have unexpected results.

    The solution is to replace your usage of arrays with suitable objects, the following code is for PHP 7 (pthreads v3+):

    class GetHostByNameCache extends Threaded {
        public function lookup(string $host) : string {
            return $this->synchronized(function() use($host) {
                if (isset($this->cache[$host])) {
                    return $this->cache[$host];
                return $this->cache[$host] = gethostbyname($host);
        private $cache = [];
    class Test extends Thread {
        public function __construct(GetHostByNameCache $cache, string $host) {
            $this->cache = $cache;
            $this->host = $host;
        public function run() {
        private $cache;
    $tests = [];
    $cache = new GetHostByNameCache();
    $domains = [
    for ($test = 0; $test < count($domains); $test++) {
        $tests[$test] = new Test($cache, $domains[$test]);
    foreach ($tests as $test)

    This will yield something like:

    string(14) ""
    string(14) ""
    string(14) ""
    string(14) ""
    string(12) ""
    string(14) ""
    object(GetHostByNameCache)#1 (1) {
      object(Volatile)#2 (6) {
        string(14) ""
        string(14) ""
        string(14) ""
        string(14) ""
        string(12) ""
        string(14) ""

    The important things to notice are:

    • The cache object is Threaded.
    • The array that appears to be used in the cache is cast to Volatile.
    • The lookup routine logic is synchronized.

    Because lookup is synchronized, not more than one thread can perform a lookup at a time, this ensures no two threads can perform the same lookup twice. You might be able to come up with a more efficient way of synchronizing access to the cache (on a per record basis), but this is a good starting place.

    点赞 评论