我有一个调用另外两个函数的函数:
class myClass{
function myFunc(){
for($i=0;$i<500;$i++){
$this->func1();
$this->func2();
}
}
function func1(){
// Does some stuff
// echos statements, and can vary the amount of echoed statements
while($something == true)
echo "This is from func1, and may echo 0-1000 times";
}
function func2(){
// Does some stuff
// echos statements, and can vary the amount of echoed statements
while($something == true)
echo "This is from func2, and may echo 0-1000 times";
}
}
我想要做的是找出一种方法,我可以获得函数回显的总时间,并在myFunc()中显示该信息。我写了一个计数函数,但它没有达到我预期的方式。
有什么建议吗?
答案 0 :(得分:4)
这是一种方式:
class myClass{
private $count;
function myFunc(){
for($i=0;$i<500;$i++){
$this->func1();
$this->func2();
}
}
function func1(){
// Does some stuff
// echos statements, and can vary the amount of echoed statements
while($something == true) {
$this->count++;
echo "This is from func1, and may echo 0-1000 times";
}
}
function func2(){
// Does some stuff
// echos statements, and can vary the amount of echoed statements
while($something == true) {
$this->count++;
echo "This is from func2, and may echo 0-1000 times";
}
}
}
或更好的方式:
class myClass{
private $count;
function myFunc(){
for($i=0;$i<500;$i++){
$this->func1();
$this->func2();
}
}
function func1(){
// Does some stuff
// echos statements, and can vary the amount of echoed statements
while($something == true) {
echoMe("This is from func1, and may echo 0-1000 times");
}
}
function func2(){
// Does some stuff
// echos statements, and can vary the amount of echoed statements
while($something == true) {
echoMe("This is from func2, and may echo 0-1000 times");
}
}
function echoMe($msg) {
echo $msg;
$this->count++;
}
}
答案 1 :(得分:1)
function myFunc(){
$echo_count = 0;
for($i=0;$i<500;$i++){
$echo_count += $this->func1();
$echo_count += $this->func2();
}
echo $echo_count;
}
function func1(){
// Does some stuff
// echos statements, and can vary the amount of echoed statements
$count = 0;
while($something == true){
echo "This is from func1, and may echo 0-1000 times";
$count++;
}
return $count;
}
function func2(){
// Does some stuff
// echos statements, and can vary the amount of echoed statements
$count = 0;
while($something == true){
echo "This is from func2, and may echo 0-1000 times";
$count++;
}
return $count;
}
答案 2 :(得分:0)
为什么不让函数返回echo数:
$echoCount = 0;
while ($something == true) {
echo "This is from func1, and may echo 0-1000 times";
$echoCount++;
}
return $echoCount;
然后在myFunc中,你可以积累它们:
function myFunc() {
$totalEchoes = 0;
for ($i=0; $i<500; $i++) {
$totalEchoes += $this->func1() + $this->func2();
}
}