如何在其他类中调用另一个调用方法?

时间:2015-07-21 06:22:22

标签: php codeigniter

请先阅读我的问题,不要将其标记为重复。在codeigniter等许多框架中,我看到他们称核心类方法为

$this->load->view (); 

所以我试图用核心PHP来实现它。我有两个类test和test2。我如何将测试方法称为test2,如

$this->test->index();

3 个答案:

答案 0 :(得分:0)

能够将方法从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();

}

答案 1 :(得分:0)

public function test1()
{
    $total = $this->test2();//calling test2() function
    echo $total;
}

public function test2()
{
    $sum = 1+2;
    return $sum;
}

答案 2 :(得分:0)

请试试.....

<?php
    $result = test2();
    function test1()
    {
        $sum = 1+2;
        return $sum;
    }

    function test2()
    {
        $total = test1();//calling test2() function
        echo $total;
    }
    ?>
相关问题