当我尝试登录我的项目时,我陷入了一个项目 它显示了我
致命错误:在非对象中调用成员函数getid() 第31行的C:\ wamp \ www \ p3 \ controller \ backend.php
我认为这是与我的backend.php文件+ AdminManager.php文件+ Author.php文件有关的问题
这是我的backend.php文件
enter code here
<?php
// Chargement des classes
require_once('model/PostManager.php');
require_once('model/CommentManager.php');
require_once('model/AdminManager.php');
//use \wwww\p3\model\AdminManager;
function login()
{
require('view/frontend/connectView.php');
}
function connexion($pseudo,$motdepasse)
{
//$postManager = new www\p3\model\PostManager();
// Création d'un objet
$adminManager = new www\p3\model\AdminManager();
//$adminManager = new AdminManager();
$resultat = $adminManager->connected($pseudo,$motdepasse);
if (!$resultat)
{
echo 'Mauvais identifiant ou mot de passe !';
}
else
{
$_SESSION['id'] = $resultat->getId();
$_SESSION['pseudo'] = $resultat->getPseudo();
header('Location: index.php?action=board');
echo 'Vous êtes connecté !';
}
}
function board()
{
$commentManager = new www\p3\model\CommentManager();
$comments = $commentManager->commentaireSignal();
$postManager = new www\p3\model\PostManager();
$posts = $postManager->getPosts();
// calling the view
require('view/backend/addPostView.php');
}
function eraseComment($commentId)
{
$commentManager = new www\p3\model\CommentManager();
$affectedLines = $commentManager->erase($commentId);
if ($affectedLines === false) {
throw new Exception('commentaire déja effacé !');
}
else {
header('Location: index.php');
}
}
function moderateComment($commentId)
{
$commentManager = new www\p3\model\CommentManager();
$affectedLines = $commentManager->moderate($commentId);
if ($affectedLines === false) {
throw new Exception('commentaire déja modéré !');
}
else {
header('Location: index.php');
}
}
function logout() {
// Suppression des variables de session et de la session
//$_SESSION = array();
session_destroy();
// Suppression des cookies de connexion automatique
header('Location: index.php');
}
/**
* method in call from the rounting page under action addPost
* @param $titre
* @param $contenu
*/
function addPost($titre, $contenu)
{
$PostManager = new www\p3\model\PostManager();
$affectedLines = $PostManager->addPost($titre, $contenu);
if ($affectedLines === false) {
throw new Exception('Impossible d\'ajouter le chapitre !');
}
else {
header('Location: index.php?action=post&id=' . $postId);
}
}
function erasePost($postId)
{
$postManager = new www\p3\model\PostManager();
$affectedLines = $postManager->deletePost($postId);
if ($affectedLines === false) {
throw new Exception('article déja effacé !');
}
else {
header('Location: index.php');
}
}
function modifyPost($postId)
{
$postManager = new www\p3\model\PostManager();
$post = $postManager->getPost($postId);
require('view/backend/updatePostView.php');
}
function domodifyPost($id,$titre,$contenu)
{
$PostManager = new www\p3\model\PostManager();
$affectedLines = $PostManager->modifyPost($id,$titre,$contenu);
if ($affectedLines === false) {
throw new Exception('Impossible d\'ajourner le chapitre !');
}
else {
header('Location: index.php?action=board' );
}
}
这是我的AdminManager.php文件
<?php
namespace www\p3\model;
require_once("model/Manager.php");
require_once("model/Author.php");
class AdminManager extends Manager
{
public function connected ($pseudo,$motdepasse)
{
$db= $this->dbConnect();
$req = $db->prepare('SELECT id,nom,prenom,pseudo,motdepasse FROM auteur
WHERE pseudo=:pseudo AND motdepasse=:motdepasse');
$req->execute(array('pseudo' => $pseudo,'motdepasse' => $motdepasse));
$req->setFetchMode(\PDO::FETCH_CLASS|\PDO::FETCH_PROPS_LATE, "Author");
$resultat = $req->fetch();
return $resultat;
}
}
这是我的Author.php文件
<?php
namespace www\p3\model;
/**
* Classe which represents an author
* @author David P.
* @version 0.1.0
*/
class Author
{
/**
* @var $id
*/
private $id;
private $firstname;
private $lastname;
private $pseudo;
private $password;
// SETTERS
public function setId($id)
{
$this->id = (int)$id;
}
public function setFirstname($firstname)
{
$this->firstname = $firstname;
}
public function setLastname($lastname)
{
$this->lastname = $lastname;
}
public function setPseudo($pseudo)
{
$this->pseudo = $pseudo;
}
public function setPassword($password)
{
$this->password = $password;
}
//GETTERS
public function getId()
{
return $this->id ;
}
public function getFirstname()
{
return $this->firstname ;
}
public function getLastname()
{
return $this->lastname ;
}
public function getPseudo()
{
return $this->pseudo ;
}
public function getPassword()
{
return $this->password ;
}
}
?>
答案 0 :(得分:0)
返回$ resultat; -不返回对象。
答案 1 :(得分:0)
我认为这可能会导致包含和要求
当可能需要多次包含被调用文件时,require_once()语句可用于在另一个文件中包含一个php文件。如果发现该文件已被包含,则调用脚本将忽略其他包含。 当您可能需要多次包含被调用文件时,include_once()语句可用于在另一个文件中包含一个php文件。如果发现该文件已被包含,则调用脚本将忽略其他包含的文件。
如果a.php是一个使用require_once()语句调用b.php的php脚本,但未找到b.php,则a.php停止执行会导致致命错误。
<?php
namespace www\p3\model;
include_once("model/Manager.php");
include_once("model/Author.php");
class AdminManager extends Manager
{
public function connected ($pseudo,$motdepasse)
{
$db= $this->dbConnect();
$req = $db->prepare('SELECT id,nom,prenom,pseudo,motdepasse FROM auteur
WHERE pseudo=:pseudo AND motdepasse=:motdepasse');
$req->execute(array('pseudo' => $pseudo,'motdepasse' => $motdepasse));
$req->setFetchMode(\PDO::FETCH_CLASS|\PDO::FETCH_PROPS_LATE, "Author");
$resultat = $req->fetch();
return $resultat;
}
}
还要更改这些:-
include_once('model/PostManager.php');
include_once('model/CommentManager.php');
include_once('model/AdminManager.php');