2014-03-06 30 views
0

我开发了一个学校项目的网站,我有一个我不明白的错误。

我有一个抽象类和一个子类。抽象类实现了一个接口,所以我可以在我的子类中使用静态方法。

控制器使用了子类。

控制器:

// CODE HERE 
case "modifier" : 
    $titre = "Modifier un bookmark"; 
    /* on vérifie que l'URL nous a bien transmis l'identifiant */ 
    if (isset($_GET['id'])) { 
     $id = $_GET['id']; 
     /* créer un bookmark à partir des infos de la BD */ 
     $jeu = Jeu_Bd::lire($id); 
     /* afficher le formulaire */ 
     $form = new Jeu_Form($jeu); 
     $c = $form->makeForm(PUBLIC_URL . "index.php?a=enregistrermodif", "modifier"); 
    } else { 
     /* on ne peut pas effectuer de modification s'il n'y a pas d'identifiant */ 
     $c = "Il manque un id."; 
    } 
    break; 
case "enregistrermodif": 
    $titre = "Jeu enregistré"; 
    /* créer un bookmark à partir des infos du formulaire et des infos de la BD */ 
    Outils_Chaines::htmlEncodeArray($data); 
    $jeu = Jeu_Bd::lire($data['id']); 
    echo(" Nom : " . $jeu->getNomJeu() . "<br/> ID : " . $jeu->getId() . "<br/> Date de Sortie : " . $jeu->getDateSortie()); 
    $jeu->update($data); 
    echo gettype($jeu); 
    $form = new Jeu_Form($jeu); 
    /* si le formulaire est valide */ 
    if ($form->verifier()) { 
     /* alors enregistrer le bookmark */ 
     Jeu_Bd::enregistrerModif($jeu); 
     $ui = new Jeu_Ui($jeu); 
     $c = $ui->makeHtml(); 
    } else { 
     /* sinon re-afficher le formulaire */ 
     $c = $form->makeForm(PUBLIC_URL . "index.php?a=enregistrermodif", "modifier"); 
    } 
    break; 
// CODE HERE 

接口:

interface IAbstractCRUD 
{ 
    /* Initialisation d'un AbstractCRUD */ 
    public static function initialize($data = array()); 

    public static function update($data); 

} 

抽象类:

abstract class AbstractCRUD implements IAbstractCRUD 
{ 
    abstract protected function __construct($map); 
} 

亚纲:

class Jeu extends AbstractCRUD 
{ 
// CODE HERE 
protected function __construct($map) 
{ 
    /* affectation des valeurs contenues dans le tableau $map */ 
    $this->id = $map['id']; 
    $this->nomJeu = $map['nomJeu']; 
    $this->description = $map['description']; 
    $this->dateSortie = $map['dateSortie']; 
} 
public static function initialize($data = array()) 
{ 
    /* créer le tableau de map*/ 

    /* id présent dans $data on non ? */ 
    if (isset($data['id'])) { 
     $map['id'] = $data['id']; 
    } else { 
     $map['id'] = ""; 
    } 

    /* titre présent dans $data ? */ 
    if (isset($data['nomJeu'])) { 
     $map['nomJeu'] = $data['nomJeu']; 
    } else { 
     $map['nomJeu'] = ""; 
    } 

    /* description présent dans $data ? */ 
    if (isset($data['description'])) { 
     $map['description'] = $data['description']; 
    } else { 
     $map['description'] = ""; 
    } 

    /* dateSortie présent dans $data ? */ 
    if (isset($data['dateSortie'])) { 
     $map['dateSortie'] = $data['dateSortie']; 
    } else { 
     $map['dateSortie'] = ""; 
    } 

    /*retourner une instance de Jeu*/ 
    return new self($map); 
} 
public static function update($data) 
{ 
    /* titre présent dans $data ? */ 
    if (isset($data['nomJeu'])) { 
     $this->setNomJeu($data['nomJeu']); ------> ERROR HERE (Fatal error: Using $this when not in object context) <------ 
    } 

    /* dateSortie présent dans $data ? */ 
    if (isset($data['dateSortie'])) { 
     $this->setDateSortie($data['dateSortie']); 
    } 

    /* description présent dans $data ? */ 
    if (isset($data['description'])) { 
     $this->setDescription($data['description']); 
    } 
} 
// CODE HERE 

我是新来的PHP,我不明白我该如何解决这个问题。我读过它是由静态方法引起的。

当我做echo $jeu->getNomJeu()我得到了我想要的东西,为什么我不能使用$这个?

+1

请阅读[关于PHP中的基本OOP](http://uk1.php.net/oop5.basic),然后[关于类中的static关键字](http://uk1.php。净/手动/ EN/language.oop5.static.php)。这一切都在手册中解释。 – naththedeveloper

+0

我已经解决了我的问题。我已经在抽象类'抽象函数更新($ data);'中放置了'public static function update($ data);'(Interface)。 – Oyabi

回答

2

使用静态方法/属性,您应该使用self。

echo self::getNomJeu(); 
相关问题