如何编写能够协同工作的简短PHP函数

如何编写能够协同工作的简短PHP函数,php,encapsulation,Php,Encapsulation,我想模块化功能,但这不起作用 class Review { public function show_report($db, $id){ // Query the DB on $id $x = $this->get_survey($db, 1); $y = $this->get_survey($db, 2); // Use x and y to build a report return $a_r

我想模块化功能,但这不起作用

class Review {
    public function show_report($db, $id){
        // Query the DB on $id
        $x = $this->get_survey($db, 1);
        $y = $this->get_survey($db, 2);
        // Use x and y to build a report
        return $a_report;
    }
    private function get_survey($db, $n){
        // Query the DB for a certain survey number
        if($n == 1){
            // Perform some logic
        } else {
            // Perform some other logic
        }
        return $a_survey;
    }
};
像这样使用类

<?php
    include_once('api/Review.class.php');
    $r = new Review();
?>
<p>
<?php
    echo Review::show_report($db, $id);
?>
</p>

谢谢你的帮助

您的设计模式很好,只是有语法错误。您在show_report()中的方法调用中错过了$符号,它应该如下所示:

public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
echo $r->show_report($db, $id);
public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
此外,类末尾的分号是不必要的

最后,正如另一个人提到的,您需要使用参数调用show_report,如下所示:

public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
echo $r->show_report($db, $id);
public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}

您的设计模式很好,只是有一个语法错误。您在show_report()中的方法调用中错过了$符号,它应该如下所示:

public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
echo $r->show_report($db, $id);
public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
此外,类末尾的分号是不必要的

最后,正如另一个人提到的,您需要使用参数调用show_report,如下所示:

public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
echo $r->show_report($db, $id);
public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}

您的设计模式很好,只是有一个语法错误。您在show_report()中的方法调用中错过了$符号,它应该如下所示:

public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
echo $r->show_report($db, $id);
public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
此外,类末尾的分号是不必要的

最后,正如另一个人提到的,您需要使用参数调用show_report,如下所示:

public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
echo $r->show_report($db, $id);
public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}

您的设计模式很好,只是有一个语法错误。您在show_report()中的方法调用中错过了$符号,它应该如下所示:

public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
echo $r->show_report($db, $id);
public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
此外,类末尾的分号是不必要的

最后,正如另一个人提到的,您需要使用参数调用show_report,如下所示:

public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}
echo $r->show_report($db, $id);
public function show_report($db, $id){
    // Query the DB on $id
    $x = $this->get_survey($db, 1);
    $y = $this->get_survey($db, 2);
    // Use x and y to build a report
    return $a_report;
}

函数
show_report($db,$id)
中是
这个
指针,没有前缀
$
符号,这会导致语法错误。此外,在第二部分中,函数不使用参数调用。 函数必须如下所示:


函数
show_report($db,$id)
中是
这个
指针,没有前缀
$
符号,这会导致语法错误。此外,在第二部分中,函数不使用参数调用。 函数必须如下所示:


函数
show_report($db,$id)
中是
这个
指针,没有前缀
$
符号,这会导致语法错误。此外,在第二部分中,函数不使用参数调用。 函数必须如下所示:


函数
show_report($db,$id)
中是
这个
指针,没有前缀
$
符号,这会导致语法错误。此外,在第二部分中,函数不使用参数调用。 函数必须如下所示:

在本例中,您试图调用不带参数的函数。如果这真的是你正在做的,那至少是一个问题

相反,使用参数调用函数:

echo $r->show_report('foo', 1);
在本例中,您试图调用不带参数的函数。如果这真的是你正在做的,那至少是一个问题

相反,使用参数调用函数:

echo $r->show_report('foo', 1);
在本例中,您试图调用不带参数的函数。如果这真的是你正在做的,那至少是一个问题

相反,使用参数调用函数:

echo $r->show_report('foo', 1);
在本例中,您试图调用不带参数的函数。如果这真的是你正在做的,那至少是一个问题

相反,使用参数调用函数:

echo $r->show_report('foo', 1);

谢谢大家。我修复了所有语法错误,多亏了。我相信这是问题的根源:

<?php
    include_once('api/Review.class.php');
    $r = new Review();
?>
<p>
<?php
    echo Review::show_report($db, $id);
?>
</p>


应该是

<?php
    include_once('api/Review.class.php');
    $r = new Review();
?>
<p>
<?php
    echo $r->show_report($db, $id);
?>
</p>



这是对静态上下文做的吗?请发表评论。

谢谢大家。我修复了所有语法错误,多亏了。我相信这是问题的根源:

<?php
    include_once('api/Review.class.php');
    $r = new Review();
?>
<p>
<?php
    echo Review::show_report($db, $id);
?>
</p>


应该是

<?php
    include_once('api/Review.class.php');
    $r = new Review();
?>
<p>
<?php
    echo $r->show_report($db, $id);
?>
</p>



这是对静态上下文做的吗?请发表评论。

谢谢大家。我修复了所有语法错误,多亏了。我相信这是问题的根源:

<?php
    include_once('api/Review.class.php');
    $r = new Review();
?>
<p>
<?php
    echo Review::show_report($db, $id);
?>
</p>


应该是

<?php
    include_once('api/Review.class.php');
    $r = new Review();
?>
<p>
<?php
    echo $r->show_report($db, $id);
?>
</p>



这是对静态上下文做的吗?请发表评论。

谢谢大家。我修复了所有语法错误,多亏了。我相信这是问题的根源:

<?php
    include_once('api/Review.class.php');
    $r = new Review();
?>
<p>
<?php
    echo Review::show_report($db, $id);
?>
</p>


应该是

<?php
    include_once('api/Review.class.php');
    $r = new Review();
?>
<p>
<?php
    echo $r->show_report($db, $id);
?>
</p>




这是对静态上下文做的吗?请发表评论。

现在我得到了一个致命错误:在Review.class.php第42行的对象上下文中不使用$this
你能发布你的完整代码吗,因为你的示例代码中没有声明变量?我试图抽象掉一些细节(这是一个很长的文件),在这样做的时候,我在准确性上做了手脚。我的错。好建议!现在我得到了一个致命的错误:在Review.class.php第42行的object context中不使用$this时,
你能发布你的完整代码吗,因为你的示例代码中没有声明变量?我试图抽象掉一些细节(这是一个长文件),在这样做的时候,我在准确性上做了手脚。我的错。好建议!现在我得到了一个致命的错误:在Review.class.php第42行的object context中不使用$this时,
你能发布你的完整代码吗,因为你的示例代码中没有声明变量?我试图抽象掉一些细节(这是一个长文件),在这样做的时候,我在准确性上做了手脚。我的错。好建议!现在我得到了一个致命的错误:在Review.class.php第42行的object context中不使用$this时,
你能发布你的完整代码吗,因为你的示例代码中没有声明变量?我试图抽象掉一些细节(这是一个长文件),在这样做的时候,我在准确性上做了手脚。我的错。好建议!这是我的问题之一。这是我的问题之一。这是我的问题之一。这是我的问题之一。这就在根本原因旁边。我在静态上下文中调用show_report。啊,您没有
echo Review::show_report($db,$id)在您的o中