如何通过另一个类的结果填充一个类中的对象数组

时间:2014-11-17 15:11:57

标签: php multithreading

我有两个课程如下

<?php
class firstClass extends Thread {
    public $Title;
public function __construct(){
    $this->Vita = "h"; //code with this line works
    $this->Vita = array();//If I define $this->Vita as an array, the variable from secondclass does not load into this variable.
}//construct
public function run(){}
public function function1(){
    $thread = new secondClass();
    $thread->start();
    $this->Vita[] = $thread->var1; // this line does not works. I cannot push into array
    //$this->Vita = $thread->var1; this line works
    print_r($this->Vita);
    }//function1
}//class first
class secondClass extends firstClass {
  public $var1;
  public function __construct(){}
  public function run(){
    $this->var1 = "Something";
 }//run
 }//class fetchLink

 $firstclassObject = new firstClass;
 $firstclassObject->function1();
?>  

如您所见,我想将$ var1从第二个类传递给一个函数到第一个类。我收到了第一类$ var1的第二类,但是当Vita是类型数组时,我无法将它推入$ this-&gt; Vita变量。如果Vita不是数组,它可以正常工作。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我可以设法解决问题。似乎在一个扩展Thread的类中,没有支持修改数组的内容。

所以,我构建了另一个不在其他类之上扩展Thread的类,如下所示:

<?php
class main {
public $arry = array();

function one(){
 $thread = new secondClass();
 $thread->start();
 $arry[] = $thread->Vita;
print_r($arry);
}//function one
}//class main

$mainobj = new main;
$mainobj->one();

class firstClass extends Thread {
    public $Title;
public function __construct(){
    $this->Vita = "h"; //code with this line works
}//construct
public function run(){}
public function function1(){
    }//function1
}//class first
class secondClass extends firstClass {
public $var1;
public function __construct(){}

public function run(){
    $this->var1 = "HHHHHH";
    $this->Vita = $this->var1;
}//run
}//class fetchLink

$firstclassObject = new firstClass;
$firstclassObject->function1();
?>  

我从称为main的外部clas调用踏板,并从线程接收值。 这样就行了。