Php 通过类将变量传递到包含的文件

Php 通过类将变量传递到包含的文件,php,Php,我在template.class.php上包含了一个文件: class template { function loadtemplate() { include_once("file.tpl"); } } $template = new template(); <?php $template->loadtemplate(); ?> 之后,我使用(在index.php中)创建了该类的一个新对象: 然后

我在template.class.php上包含了一个文件:

    class template {
        function loadtemplate() { 
            include_once("file.tpl");
        }
    }
$template = new template();
<?php $template->loadtemplate(); ?>
之后,我使用(在index.php中)创建了该类的一个新对象:

然后我执行了该方法,以包含该文件(在index.php中):


现在我想在我的文件.tpl中执行index.php中定义的函数

错误:未定义变量:var。
如何传递变量?如果我不使用该方法而直接在index.php中包含该文件,则会传递变量。

我自己找到了一个解决方案:

function loadtemplate($var) {...}
index.php中

<?php $template->loadtemplate($var); ?>