当对象变量相同时,调用not对象上的成员函数


Call to a member function on a not object when the object variable is the same

你好,我有这个问题:我有一个php文件,里面有我所有的函数和所有的类,在这个文件里我有一个子类Studente:

 class Studente implements JsonSerializable{
    private $codiceFiscale; 
    private $nome;
    private $cognome;

function __construct($codiceFiscale,$nome,$cognome) {
        $this->codiceFiscale=$codiceFiscale;
        $this->nome=$nome;
        $this->cognome=$cognome;
    }
public function getCodiceFiscale(){
    return $this->codiceFiscale;
}
public function getNome(){
    return $this->nome;
}
public function getCognome(){
    return $this->cognome;
}

然后我有一个函数,它接收了正确创建的studente对象的距离。问题是如何将变量$studente称为

function test ($studente){
$studente=$studente->getCodiceFiscale(); // this generates the error "call on a non //object...." and i dont't understand why
$std=$studente->getCodiceFiscale(); //NO ERROR
}

所以我不明白为什么我不能使用相同的变量;方法getCodiceFiscale()返回一个字符串。

THX寻求帮助

为了完成名为"responseRegistroDocente"的页面上的代码,它在第一行中包括我拥有的所有功能的文件:

elseif (isset($_POST['operazione']) && $_POST['operazione']==='calcolaMedia' && isset($_POST['nomeStudente']) && isset($_POST['dataInizio']) && isset($_POST['dataFine']) && isset($_POST['tipoProva'])){
    try{
        $regExp='/^(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)'d'd$/';
        $dataInizio=null;
        $dataFine=null;
        $studente=new Studente($_POST['nomeStudente'],null,null);
        $tipoProva=$_POST['tipoProva'];
        $test=true;//preg_match($regExp,'scemo');
        if(!$test){
            throw new Exception ("La data di inizio non rispetta il formato GG/MM/YYYY.");
        }
        else{
            $test=true;//preg_match($regExp,$_POST['dataFine']);
            if(!$test){
                throw new Exception("La data di fine non rispetta il formato GG/MM/YYYY.");
            }
            else{
                $data=str_replace("/","-", $_POST['dataInizio']);
                $dataInizio=new DateTime($data);
                $data=str_replace("/","-", $_POST['dataFine']);
                $dataFine=new DateTime($data);
                $dataFine->setTime(0,0,0);
                $dataInizio->setTime(0,0,0);
                if($dataFine<$dataInizio){
                    throw new Exception("La data di inizio deve essere minore della data di fine.");
                }
                else{
                    $medie=getMedie($conn,$studente,$dataInizio,$dataFine,$tipoProva,$_SESSION['corso'],$_SESSION['classe'],$_SESSION['codiceFiscale']);
                    if(!$medie){
                        echo("Lo Studente non ha sostenuto alcuna prova nel periodo di tempo specificato.");
                    }
                    else{
                        echo(json_encode($medie));
                    }
                }
            }
        }
    }
    catch (Exception $e){
        echo($e->getMessage());
    }
}

我有一个外部文件,上面有所有的类声明,特别是生成错误的函数

function getMedie($conn,Studente $studente,$dataInizio,$dataFine,$tipoProva,$corso,$classe,$docente){
    $votiAndMedia=array();
    $idProve=array();
    $msg='SCRITTA';
    if($tipoProva==='O'){
        $msg='ORALE';
    }
    $query="SELECT idProva FROM prova where docente_codiceFiscale='{$docente}' && materia_idMateria='{$corso}' && classe_idclasse='{$classe}' && tipoProva='{$tipoProva}';";
    $result=mysqli_query($conn,$query);
    if(!$result){
        throw new Exception("Errore nell' esecuzione della query per caricare le medie: ".mysqli_error($conn).".");
    }
    else{
        if(($nRow=mysqli_num_rows($result))===0){
            throw new Exception("Non esiste nessuna prova ".$msg." relativa a ".$corso." memorizzata nel Database.");
        }
        else{
            for($a=0;$a<mysqli_num_rows($result);$a++){
                $prove=mysqli_fetch_assoc($result);
                $idProve[$a]=$prove['idProva'];
            }
            mysqli_free_result($result);
            foreach ($idProve as $prova){
                $studente=$studente->getCodiceFiscale();
                $query="SELECT data,voto FROM sostieneprova where studente_codiceFiscale='{$codf}' && prova_idprova=$prova && data>=? && data<=?;";
                $stmt=mysqli_prepare($conn,$query);
                if(!$stmt){
                    throw new Exception("Errore nell' esecuzione della query per caricare le medie: ".mysqli_error($conn).".");
                }
                else{
                    $dataI=$dataInizio->format("Y-m-d");
                    $dataF=$dataFine->format("Y-m-d");
                    mysqli_stmt_bind_param($stmt,"ss",$dataI,$dataF);
                    $successo=mysqli_stmt_execute($stmt);
                    if(!$successo){
                        throw new Exception(mysqli_stmt_error($stmt));
                    }
                    else{
                        mysqli_stmt_store_result($stmt);
                        if(($nRow=mysqli_stmt_num_rows($stmt))===0){
                        //NON HA SOSTENUTO ANCORA LA PROVA
                        }
                        else{
                            $data=null;
                            $voto=null;
                            mysqli_stmt_bind_result($stmt,$data,$voto);
                            while (mysqli_stmt_fetch($stmt)){
                                $obj= new stdClass();
                                $obj->data=$data;
                                $obj->voto=$voto;
                                $votiAndMedia[]=$obj;
                        }
                        mysqli_stmt_free_result($stmt);
                    }
                }
            }
          }
       }
       }
       usort($votiAndMedia,function (stdClass $a,stdClass $b){
        $data1=new DateTime($a->data);
        $data1->setTime(0,0,0);
        $data2=new DateTime($b->data);
        $data2->setTime(0,0,0);
        if($data1<$data2){
            return -1;
        }
        elseif ($data1==$data2) {
            return 0;
        }
        else{
            return 1;
            }
       });
    return $votiAndMedia;

}

好吧,现在的问题是,在函数中,我不能写这个代码:

$studente=$studente->getCodiceFiscale();

以及CCD_ 1以及$dataFine=$dataFine->format("d-m-Y);

但是,如果我更改等号之前的所有变量名称,就不会有错误。为什么?

好吧。。。我已经想通了。

您使用的是foreach循环,如下所示:

foreach ($idProve as $prova){
 $studente=$studente->getCodiceFiscale();

正如您所说,getCodiceFiscale()返回字符串,当再次调用它时,其中没有对象,而是字符串,并且您不能在类型string上调用此方法。

此代码运行良好(PHP 5.5.3-1ubuntu2):

class Studente {
    private $codiceFiscale;
    private $nome;
    private $cognome;

    function __construct($codiceFiscale,$nome,$cognome) {
        $this->codiceFiscale=$codiceFiscale;
        $this->nome=$nome;
        $this->cognome=$cognome;
    }
    public function getCodiceFiscale(){
        return $this->codiceFiscale;
    }
    public function getNome(){
        return $this->nome;
    }
    public function getCognome(){
        return $this->cognome;
    }
}
$studente = new Studente(1, 1, 1);
test($studente);
function test ($studente){
    $studente=$studente->getCodiceFiscale(); // this generates the error "call on a non //object...." and i dont't understand why
    echo $studente;
}