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);?
我只想问一下如何回显像这个印刷品的功能(h(pow);?
1 个解决方案
#1
0
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 ...
致命错误:在...中调用未定义的函数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, 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!
这是基本的OOP PHP。另外我强烈建议你使用更有意义的函数和变量名!
#1
0
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 ...
致命错误:在...中调用未定义的函数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, 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!
这是基本的OOP PHP。另外我强烈建议你使用更有意义的函数和变量名!