0

I've created a simple class:

class Event {


   var $a;
   var $b;
   var $c;


   function __construct($a, $b, $c) 
   {
      $this->a= $a;
      $this->b = $b;
      $this->c= $c;
   }



  } 

Then I've created another class which extends Thread:

class WorkerThread extends Thread
{
    private $myUrl;
    private $eventsArr;
    private $counterDebug;
    private $postdata;

    public function __construct($myUrl, $postdata)
    {
        $this->myUrl = $myUrl;
        $this->postdata = $postdata;
        $this->eventsArr = array();
        $this->counterDebug=0;
    }

    public function run()
    {
          $flag=false;

          foreach ($json as $key => $value) {


                    $this->counterDebug++;
                    /* Death event */
                    $event= new Event($a, $b, $c);
                    array_push($this->eventsArr, $event);

                }
              }

            }
          }
          echo (count($this->eventsArr));
          echo (json_encode($event));
          echo ("\n" . $this->counterDebug);

    if($flag && count($this->events)>0){
    ...

When trying to add new created objects into the array, it stays empty.

What I've figured from debugging:

1) The objects are created.

2) neither eventsArr[]= $event, nor array_push are working.

3) I've set a counter that verifies the objects are being created and should be added to the array.

What am I doing wrong?

p.s-

I've removed irrelevant parts of code in order to simplify things.

2
  • i think you should create temp array which have all your events and then give it to $eventsArr Commented Dec 19, 2015 at 1:00
  • 1
    What problem were you trying to solve in PHP that you ended up using pthreads? They work fine but not needed that often? As an 'experiment' also good. Except you didn't allow any other pthreads to run? pthreads have to give some result then wait until they get control back? Commented Dec 19, 2015 at 1:30

1 Answer 1

2

i think you should create temp array which have all your events and then give it to $eventsArr like this.

$temp = array();
foreach ($json as $key => $value) {
    $this->counterDebug++;
    /* Death event */
    $event= new Event($a, $b, $c);
    $temp[] = $event;
}
$this->eventsArr = $temp;

OR

foreach ($json as $key => $value) {
    $this->counterDebug++;
    /* Death event */
    $event= new Event($a, $b, $c);
    $this->eventsArr[] = $event;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks! It works :) Could you please explain why it works only this way?
i think you should pass the array once to $eventsArr.
Please check the update of my answer. it will explain to you why eventsArr[] not worked with you.
I've tried the second option.. It doesn't work.. only the $temp works.. I still don't get what's wrong with the second option
can you please post your second option to let me check it
|

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.