簡體   English   中英

如何在PHP中回顯此打印(h(pow)的功能?

[英]how to echo function in php like this print(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());

我只想問一下如何回顯類似print(h(pow);?

首先使用以下命令打開error reporting

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

您會看到(除了錯別字):

致命錯誤:在...中調用未定義的函數h()

那是因為你有一個帶有方法的類。 因此,您必須將您的類的實例作為調用該方法的方法,例如

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

這是基本的OOP PHP 另外,我強烈建議您使用更有意義的函數和變量名!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM