0

I developp a website for school project and I have an error I don't understand.

I have an abstract class and a subclass. Abstract class implements an interface so I can use static method in my subclass.

A controller used subclass.

Controller :

// 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 :

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

    public static function update($data);

}

Abstract Class :

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

Subclass :

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

I'm new in php and I don't understand how can I do for resolve that problem. I have read it's cause by static method.

When I do echo $jeu->getNomJeu() I get what I want so why I can't use $this ?

2

2 Answers 2

4

With static methods / attributes you should use self.

echo self::getNomJeu();
2

From http://www.phpbuilder.com/board/showthread.php?t=10354489:

Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members.

Not the answer you're looking for? Browse other questions tagged or ask your own question.