Ajax调用工作正常,但php文件在从对象检索属性时出错


Ajax call works properly but php file get error when retrieving attributes from object

这一定是一个简单的语法错误,但我无法得到。所以在我的HTML中,我有一些空标签(tbody)

    <label for="matiere2">Classe :</label>
                <select name="matiere2" id="matiere1" onchange="showList(this.value)">
                    <option></option>
                    '.$this->addClasseToselect().'
                </select>
            <div class="tableau">
            <table>
                <caption>Mes listes</caption>
                <thead>
                    <tr>
                        <th>Quantité</th>
                        <th>Libellé</th>
                        <th>Supprimer</th>
                    </tr>
                </thead>
                <tbody id="tbody">
                </tbody>
            </table>

我还使用onchange ajax函数进行选择,以便在表中获得正确的数据这是我的js文件,只有一个ajax函数

    function showList(str) {
       if (str == "") {
        document.getElementById("tbody").innerHTML = "";
        return;
      } else {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        var versions = ["MSXML2.XmlHttp.5.0",
            "MSXML2.XmlHttp.4.0",
            "MSXML2.XmlHttp.3.0",
            "MSXML2.XmlHttp.2.0",
            "Microsoft.XmlHttp"]
        for(var i = 0, len = versions.length; i < len; i++) {
            try {
                xmlhttp = new ActiveXObject(versions[i]);
                break;
            }
            catch(e){}
        } // end for
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("tbody").innerHTML = xmlhttp.responseText;
        }
    };
    xmlhttp.open("GET","vue/getliste.php?q="+str,true);
    xmlhttp.send();
}

}函数中调用的php文件(getliste.php):

    <?php
    include_once ('../modele/liste.php');
    $q = intval($_GET['q']);
    $res='';
    foreach($listes=liste::findAllByIdAndClasse($q) as $c){
    var_dump($c);
    $res.='<tr><td>'.$c->__get['quantite'].'</td><td>'.$c->__get['intitule'].'</td><td><a class="del_button" type="button" onclick="document.getElementById(''light'').style.display=''block'';document.getElementById(''fade'').style.display=''block''" id="">X</a></td></tr>';
    }
    echo $res;

以下是我从var_dump()中得到的内容,因此我的数据被正确加载:

    object(liste)[3]
    private 'id' => string '23' (length=2)
    private 'quantite' => string '7' (length=1)
    private 'intitule' => string 'Mcdo' (length=4)
    private 'idClasse' => string '18' (length=2)
    private 'idMatiere' => string '11' (length=2)
    private 'idProf' => string '53' (length=2).

下面是我的类"liste"及其属性、__get方法和getliste.php中使用的findAllByIdAndClasse(id)方法类别列表{

private $id;
private $quantite;
private $intitule;
private $idClasse;
private $idMatiere;
private $idProf;
function __construct()
{
}

public function __get($attr_name) {
    if (property_exists( __CLASS__, $attr_name)) {
        return $this->$attr_name;
    }
    $emess=__CLASS__ . ": unknown member $attr_name (getAttr)";
    throw new Exception($emess, 45);
}

和findAllByIdAndName(id),仍在liste.php 中

    public static function findAllByIdAndClasse($q){
    session_start();
    $c=base::getConnection();
    $query=$c->prepare("select * from liste WHERE idProf=:idProf AND idClasse=:idClasse ORDER BY idClasse ASC ");
    $query->bindParam(':idProf',$_SESSION['userid'],PDO::PARAM_INT);
    $query->bindParam(':idClasse',$q,PDO::PARAM_INT);
    $dbres=$query->execute();
    $d=$query->fetchAll();
    $tab=array();
    foreach ($d as $key => $value) {
        $a=new liste();
        $a->id=$value['id'];
        $a->quantite=$value['quantite'];
        $a->intitule=$value['intitule'];
        $a->idClasse=$value['idClasse'];
        $a->idProf=$value['idProf'];
        $a->idMatiere=$value['idMatiere'];
        $tab[]=$a;

    }
    return $tab;
}

我收到一个php错误,说我正在尝试获取一个不存在的属性,我检查了它的拼写,它似乎是正确的,我唯一的线索是这是一个语法错误,有一些"问题,但我可以"获得它…任何帮助,谢谢!

编辑

所以现在我尝试在if之前的__get函数中添加一个var_dump,所以它变成了:

    public function __get($attr_name) {
var_dump($attr_name);
if (property_exists( __CLASS__, $attr_name)) {
    return $this->$attr_name;
}
$emess=__CLASS__ . ": unknown member $attr_name (getAttr)";
throw new Exception($emess, 45);

}我从中得到的结果是:

string '__get' (length=5)

所以它的意思是这条线:

$res.='<tr><td>'.$c->__get['quantite'].'</td><td>'.$c->__get['intitule'].'</td></tr>';

正在发送$c->__get[__get]。。。。嗯,我不明白…

最后一行代码中有一个错误:

$res.='<tr><td>'.$c->__get['quantite'].'</td><td>'.$c->__get['intitule'].'</td></tr>';

应该是。。。

$res.='<tr><td>'.$c->quantite.'</td><td>'.$c->intitule.'</td></tr>';

"__get"函数使用"magic"捕获未定义的属性。在代码中,您试图读取的未定义属性是"__get",这就是var_dump显示的内容。

查看PHP手册了解更多详细信息。。。http://php.net/manual/en/language.oop5.magic.php