简体   繁体   中英

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());

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

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, eg

$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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM