解析错误:语法错误,意外'$ Contrato'(T_VARIABLE)

时间:2014-11-24 12:06:43

标签: php class

我需要从类中获取一个多维数组,当访问结果时,我得到错误Parse错误:语法错误,指令中出现意外的'$ Contrato'(T_VARIABLE)

print_r $ Contrato-> getDatosContrato();

有什么想法吗? 这是我的代码

require_once('config.php');

class Contrato {

    public $mainContractor;
    public $MiCodigo;
    public $title;
    public $description;
    public $buyerCompany;
    public $buyerUser; 
    public $buyerUserId; 
    public $categoryCode;
    public $categoryName;

    public function getDatosContrato() {        
        $parametros = array(
                "contract"=> array(
                            "mainContractor" => array(
                                                "id" => $this->mainContractor
                                                ),
                            "MiCodigo" => $this->MiCodigo,
                            "title" => $this->title,
                            "description" => $this->description,
                            "buyerCompany" => array(
                                            "id" => $this->buyerCompany
                                            ),      
                            "buyerUser" => $this->buyerUser,
                            "buyerUserId" => $this->buyerUserId,
                            "categoryList" => array(
                                            "category" => array(
                                                        "categoryCode" => $this->categoryCode,
                                                        "categoryName" => $this->categoryName
                                                        )
                                            )       
                        )
        );
        return $parametros;
    }
}

try 
{

    $BD = new PDO("mysql:host=".DB_HOST.";dbname=".DB_DATABASE, DB_USER, DB_PASSWORD);

    $stmt = $BD->query("SELECT * FROM Contracts");

    $stmt->setFetchMode(PDO::FETCH_INTO, new Contrato);

    foreach($stmt as $Contrato)
    {
        print_r $Contrato->getDatosContrato().'<br />';
    } 

    $BD = null;
}
catch(PDOException $e)
{
    echo $e->getMessage();
}

4 个答案:

答案 0 :(得分:6)

print_r是一个功能,因此您缺少括号:

print_r($something);

答案 1 :(得分:3)

print_r需要括号:

print_r(...);

答案 2 :(得分:2)

print_r($Contrato->getDatosContrato()).'<br />';

print_recho不同。

它需要括号。

print_r()是一个函数,所以需要括号。

echo是一种语言构造,因此,您可以不使用括号(例如includerequire,...)

答案 3 :(得分:2)

以这种方式使用print_r(),

foreach($stmt as $Contrato)
{
    print_r ($Contrato->getDatosContrato());
    echo '<br />';
}