Php 如何获得count变量的输出?
调用我的类的run方法。它应该是return$count或echo$count 我不确定我是否完全理解您所说的“如何获得count变量的输出”的意思,但我发现可能会出现一些问题 1) $count是一个全局变量,而不是类或方法变量,这意味着在运行方法中仅向count添加1不会在类定义之外对$count进行任何更改 2) 我没有看到调用更新$count(尽管它在类之外)变量的run方法的任何地方 3) 即使在某个地方调用了run方法,您也必须在run方法的开头添加一行global$count 4) 我建议您在类内部存储$count,并使用函数返回它,这意味着该类如下所示:Php 如何获得count变量的输出?,php,Php,调用我的类的run方法。它应该是return$count或echo$count 我不确定我是否完全理解您所说的“如何获得count变量的输出”的意思,但我发现可能会出现一些问题 1) $count是一个全局变量,而不是类或方法变量,这意味着在运行方法中仅向count添加1不会在类定义之外对$count进行任何更改 2) 我没有看到调用更新$count(尽管它在类之外)变量的run方法的任何地方 3) 即使在某个地方调用了run方法,您也必须在run方法的开头添加一行global$count 4)
<?php
$count=0;
class My extends Thread
{
private $myid;
// ini_set('max_execution_time', 0);
//echo date("Y-m-d H:i:s")."<br/>";
public function __construct($id)
{
$this->myid = $id;
}
public function run()
{
for($t=0;$j+$t<=100;$t+=10){ //future buy
for($k=0;$j+$t+$k<=100;$k+=10){//future sell
for($l=1;$l<=14;$l++){ // strike
for($m=0;$j+$k+$m<=300;$m+=10){ //put buy
for($n=1;$n<=14;$n++){ // strike
for($o=0;$o<=300;$o+=10){ // call buy
for($p=1;$p<=14;$p++){ //strike
if($p==$l)
continue;
for($q=0;$q<=300;$q+=10){ // put sell
for($r=1;$r<=14;$r++){ // strike
if($r==$n)
continue;
for($s=0;$s<=300;$s+=10){ // call buy
$count ++;
}
}
}
}
}
}
}
}
}
}
}
}
echo date("Y-m-d H:i:s")."<br/>";
$mycalls = [];
for($i=0;$i<=100;$i+=10)
{
$mycalls[$i]= new My($i);
$mycalls[$i]->start();
$mycalls[$i]->join();
}
echo date("Y-m-d H:i:s")."<br/>";
echo "<br>";
echo $count;
?>
$myInstance = new My();
$myInstance -> run();
$someCount = $myInstance -> getCount();
然后从类实例中获取计数值:
Class My extends Thread{
protected $count = 0;
public function run(){
//...
$this -> count++;
//...
}
public function getCount(){
return $this -> count();
}
}
由于您正在创建101个my实例,因此代码如下所示:
<?php
$count=0;
class My extends Thread
{
private $myid;
// ini_set('max_execution_time', 0);
//echo date("Y-m-d H:i:s")."<br/>";
public function __construct($id)
{
$this->myid = $id;
}
public function run()
{
for($t=0;$j+$t<=100;$t+=10){ //future buy
for($k=0;$j+$t+$k<=100;$k+=10){//future sell
for($l=1;$l<=14;$l++){ // strike
for($m=0;$j+$k+$m<=300;$m+=10){ //put buy
for($n=1;$n<=14;$n++){ // strike
for($o=0;$o<=300;$o+=10){ // call buy
for($p=1;$p<=14;$p++){ //strike
if($p==$l)
continue;
for($q=0;$q<=300;$q+=10){ // put sell
for($r=1;$r<=14;$r++){ // strike
if($r==$n)
continue;
for($s=0;$s<=300;$s+=10){ // call buy
$count ++;
}
}
}
}
}
}
}
}
}
}
}
}
echo date("Y-m-d H:i:s")."<br/>";
$mycalls = [];
for($i=0;$i<=100;$i+=10)
{
$mycalls[$i]= new My($i);
$mycalls[$i]->start();
$mycalls[$i]->join();
}
echo date("Y-m-d H:i:s")."<br/>";
echo "<br>";
echo $count;
?>
$myInstance = new My();
$myInstance -> run();
$someCount = $myInstance -> getCount();
$mycalls=[];
$count=0;
对于($i=0;$istart();
$mycalls[$i]->join();
$count+=$mycalls[$i]->getCount();
}
echo$count;
此计划是否需要建造师?