您好我有这个问题:我有一个包含我所有函数和所有类的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对象的istance。问题在于我如何调用变量$ 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 FOR HELP
for completition名为“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();
以及$dataInizio=$dataInizio->format("d-m-Y");
还有$dataFine=$dataFine->format("d-m-Y);
但如果我在等号前更改变量的所有名称,则没有错误。为什么??
答案 0 :(得分:0)
此代码工作正常(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;
}
答案 1 :(得分:0)
好吧......我已经明白了。
您正在使用foreach
循环,如下所示:
foreach ($idProve as $prova){
$studente=$studente->getCodiceFiscale();
就像你说的那样,getCodiceFiscale()
返回字符串,当它再次被调用时,它中没有对象,但是字符串,你不能在类型string
上调用这个方法。