将Var从类传递到包含

时间:2011-10-24 23:24:40

标签: php function variables include var

我有一个函数,我想在其中使用include,但是我需要将Class中公开的所有vars干净地传递给页面布局包含文件。

vars传递给函数内部,但传递给include时则不会。这样做有什么简单的建议吗?

private function printGraph() {
    /*
    if($_SERVER['HTTP_HOST']=='localhost') {
        echo "<pre>\n";
        echo "Actual: ".$this->actual."\n";
        echo "Actual Bar: ".$this->actualbar."\n";
        echo "Attainable Bar: ".$this->attainablebar."\n";
        echo "Attainable: ".$this->attainable."\n";
        echo "ADSL2Calc: ".$this->adsl2calc."\n";
        echo "</pre>";
    }
    */

    //Adding the new look Tshooterlayout!
    include 'tshooterlayout14.php';

    // Actual Speed info and bar
    $actual=file_get_contents("inc/troubleshooter/tshoot3-actual.inc");
    $actual=sprintf($actual,$this->actual,$this->actualbar,$this->attainablebar);

1 个答案:

答案 0 :(得分:1)

只需使用$ this:

<强> test.php的

<?php
class Test {
    public $foo = 'bar';

    public function testme(){
        include "include.php";
    }

}

$T = new Test();

$T->testme();

<强> include.php

<?php
echo "You got your " . $this->foo ." in my foo!\n";

<强>结果

$ php test.php
You got your bar in my foo!