PHP:Public Function变量在外面不可见

时间:2014-07-10 12:32:45

标签: function variables public

这是示例代码:

    class PeoplePDF extends TFPDF {
    // Create basic table
    public $test;
    public function CreateTable($header, $data)
    {

        foreach ($header as $col) {
            $this->Cell($col[1], 8, $col[0], 1, 0, 'C', true);
        }
        $this->Ln();
        // Data
        $this->SetFillColor(255);
        $this->SetTextColor(0);
        $this->SetFont('Arial','','9');

        $date = date('d/m/y', strtotime($row["SendDate"]));

        //$allRowsCount = count($data);

        $db = new PDO('mysql:host=localhost;dbname=Service;', 'root', '', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
        $sqlNum = $db->prepare("select * From actnumbers");
        $sqlNum->execute();
        $sqlNumResult = $sqlNum->fetchAll(PDO::FETCH_ASSOC);

        $lastnumber = $sqlNumResult[0]["LastNumber"];
        //$this->test = $sqlNumResult[0]["ActNumber"];
        $this->test = "test";

        foreach ($data as $row)
        {
            $date = date('d/m/y', strtotime($row["SendDate"])); 
               $this->Cell($header[0][1], 6, $lastnumber, 1, 0, 'C', true);             
        }


    }
}

$myObj = new PeoplePDF ();
$test = $myObj->test;
echo $test; //it's not visible. echoes nothing

我知道我犯了一个非常愚蠢的错误。 我是新手。 ) 我想我没有正确地声明变量$ test; 我怎样才能做到这一点? 提前致谢

1 个答案:

答案 0 :(得分:0)

class PeoplePDF extends TFPDF {
public $test;

  public function CreateTable($header, $data){ 

    $this->test = "test";

  }

}

在课外,您可以像这样使用它

$myObj = new PeoplePDF ();
$test = $myObj->test;