请先阅读我的问题,不要将其标记为重复。在许多框架中,如codeigniter等。我看到他们调用核心类方法,比如
$this->load->view ();
所以我试图实现它与核心PHP。我有两个类测试和测试2。如何将test方法调用到test2中,如
$this->test->index();
public function test1()
{
$total = $this->test2();//calling test2() function
echo $total;
}
public function test2()
{
$sum = 1+2;
return $sum;
}
能够从类B调用方法到类a
A类必须扩展B类
class test extends test2 {
}
或者你必须有这样的东西:
class test {
function runTest2method() {
$this->test2method();
}
function test2method() {
$Test2 = new test2();
$userIt = $test2->test2classmethod();
}
}
例如,在yii2中使用一个类的方法在另一个你只是使用静态函数:
use common'models'User;
class controller {
function somefunction() {
$user = User::userFunction();
}
请尝试.....
<?php
$result = test2();
function test1()
{
$sum = 1+2;
return $sum;
}
function test2()
{
$total = test1();//calling test2() function
echo $total;
}
?>