dongqiao6730 2015-07-07 18:45
浏览 42
已采纳

如何在这个打印(h(pow)?

class fruits
{
 function g($str = 'fruits'){

$i=0;
$new_str = "";
while ($i < strlen($str)-1){
$new_str = $new_str + $str[$i+1];
$i = $i + 1;

}

return $new_str;
}

function f($str = 'fruits') {

if (strlen($str)== 0) {
return "";
}
else if  (strlen($str)== 1)
{
return $str;
}

else
{
return $this->f($this->g($str)) + $str[0]; }

}

function h($n=1, $str = 'fruits'){

while ($n != 1){

if ($n % 2 == 0){
$n = $n/2;
}
else 
{
$n = 3*$n + 1;
}
$str = $this->f($str);
}
return $str;
}

function pow($x, $y){
if (y==0)
{
return 1;
}

else 
{
return $x * $this->pow($x, $y-1);

}

}


}





$obj = new fruits;


print(h(pow());

I only want to ask how to echo a function like this print(h(pow);?

  • 写回答

1条回答 默认 最新

  • duandu6497 2015-07-07 18:49
    关注

    First turn on error reporting with:

    <?php
        ini_set("display_errors", 1);
        error_reporting(E_ALL);
    ?>
    

    And you will see (Besides the typos):

    Fatal error: Call to undefined function h() in ...

    That is because you have a class with methods. So you have to take an instance of your class an call the method from it, e.g.

    $obj = new fruits;
    echo $obj->h($obj->pow(4, 5));
    

    This is basic OOP PHP. Also I would highly recommed you to use more meaningful function and variable names!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度