drpph80800 2014-03-06 13:48
浏览 51
已采纳

在静态方法中不在对象上下文中时使用$ this

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条回答 默认 最新

  • doufang1954 2014-03-06 13:53
    关注

    With static methods / attributes you should use self.

    echo self::getNomJeu();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来