【问题标题】:Impossible to assign simple Array on Stackable无法在 Stackable 上分配简单的数组
【发布时间】:2015-01-07 15:19:28
【问题描述】:

我正在尝试为使用 pthread 的 codeigniter 创建一个库,一切正常,但是当我想为数组分配一个值时,这不适用于传统的 $a['key'] = 'val';

小测试示例:(更新)

class Test {
    protected $core;
    protected $stack;

    public function init(){
        $this->stack = new Test_Stack();
        $this->core = new Test_Core($this->stack);
    }

    public function do_test(){
        return $this->core->assign();
    }
}


class Test_Stack extends Stackable {
    protected $a;

    function __construct(){
        $this->a = array();
    }

    protected function test(){ // Call from other class extends Threads
        $this->a['key1'] = 'NOWORK';
        print_r($this->a); // THIS RETURN NOTHING   

        $this->a = array_merge($this->a, array('key1' => 'WORK'));
        print_r($this->a); // NOW THIS GOOD RETURN Key1..
    }

    public function run(){}
}

class Test_Core {
    protected $thread;
    protected $stack;

    function __construct($s){
       $this->stack = $s;
    }

    public function assign(){
        $this->thread = new Test_Thread($this->stack);
        $this->thread->start();

        $this->thread->join();
    }
}

class Test_Thread extends Thread{
    protected $stack;

    function __construct($s){
       $this->stack = $s;
    }

    public function run(){
        $this->stack->test();
    }
}

我在没有测试的情况下编写了这个基本代码,但它与我的 lib 的结构相同,并且需要它来扩展 test_stack 并添加或更改测试函数,例如。

即使现在可以工作,我也会明白为什么我不能正常分配我的数组? 或者更确切地说,我做错了什么?

【问题讨论】:

  • 对我来说很好用!我得到的输出:Array ( [key1] => NOWORK ) Array ( [key1] => WORK )
  • 好吧,这很奇怪,我将发布更像我的实现的新代码。谢谢Rizier123
  • 您是否启用了通知?
  • 是 Marek,error_reporting = E_ALL,display_error On 并且什么都没有出现

标签: php arrays codeigniter pthreads


【解决方案1】:

在我的工作:

class Stack  {
    protected $a;

    function __construct(){
        $this->a = array();
    }

    function test(){ 
         $this->a['key1'] = 'NOWORK';
         print_r($this->a); // THIS RETURN NOTHING


         $this->a = array_merge($this->a, array('key1' => 'WORK'));
         print_r($this->a); // NOW THIS GOOD RETURN Key1..
    }
}


$obj = new Stack();
$obj->test();

输出:

Array
(
    [key1] => NOWORK
)
Array
(
    [key1] => WORK
)

【讨论】:

  • Yes Tiger thx 但你不使用 Pthread 和 Stackable 类:(
【解决方案2】:

OK 数组不是线程安全的,应该使用可堆叠的,这样就可以了:

库/Test.php:

class Test {
    protected $core;
    protected $stack;

    public function init(){
        $this->stack = new Test_Stack();
        $this->core = new Test_Core($this->stack);
    }

    public function do_test(){
        $this->core->assign();
        $this->core->assign();
        $this->core->assign();
    }

    public function get_a(){
        return $this->core->get_a();
    }
}

class Test_Array_Stack extends Stackable {
    public function run(){}
}

class Test_Stack extends Stackable {
    protected function test($a){
        $a[] = 'WORK';
    }
    public function run(){}
}

class Test_Core {
    protected $thread;
    protected $stack;
    protected $a;

    function __construct($s){
       $this->stack = $s;

       $this->a = new Test_Array_Stack();
    }

    public function assign(){
        $this->thread = new Test_Thread($this->stack, $this->a);
        $this->thread->start();

        $this->thread->synchronized(function($thread){
            $thread->wait();
        }, $this->thread);

        return $this->a;
    }

    public function get_a(){
        return $this->a;
    }
}

class Test_Thread extends Thread{
    protected $stack;
    protected $a;

    function __construct($s, $a){
       $this->stack = $s;
       $this->a = $a;
    }

    public function run(){
        $this->stack->test($this->a);

        $this->synchronized(function($thread){
            $thread->notify();
        }, $this);
    }
}

您可以扩展 Test_Stack 以创建对我的项目更有用的 lib 扩展,例如在其他文件 Test_Info.php 上:

require_once APPPATH.'libraries/Test.php';

class Test_Info extends Test {

    function init(){
        $this->stack = new Test_Info_Stack();
        $this->core = new Test_Core($this->stack);
    }
}

class Test_Info_Stack extends Test_Stack {
    protected function test($a){
        parent::test($a);
        $a[] = 'INFO';
    }
}

以及在控制器上的使用:

function index(){
    //without extension
    $this->load->library('Test');
    $this->test->init();
    $this->test->do_test();
    print_r($this->test->get_a());

    //with extension
    $this->load->library('Test_Info');
    $this->test_info->init();
    $this->test_info->do_test();
    print_r($this->test_info->get_a());
}

我花了一些时间,我希望它能帮助别人,LuckyBurger 谢谢你的解释链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 2017-01-25
    相关资源
    最近更新 更多