简体   繁体   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());

I only want to ask how to echo a function like this print(h(pow);? 我只想问一下如何回显类似print(h(pow);?

First turn on error reporting with: 首先使用以下命令打开error reporting

<?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 ... 致命错误:在...中调用未定义的函数h()

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 . 这是基本的OOP PHP Also I would highly recommed you to use more meaningful function and variable names! 另外,我强烈建议您使用更有意义的函数和变量名!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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