1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

$form_templateを継承するとハマるので注意しましょう

Last updated at Posted at 2013-06-05

Ethnaでは$form_templateという便利な機能がありますが、このプロパティ自体を継承してしまうと親の$form_templateをつぶしてしまいます。

Ethna初心者がよく陥る罠なので気をつけましょう。

例えば親クラスで$form_templateを定義して、

Sample_ActionForm.php
class Sample_ActionForm extends Ethna_ActionForm
{
    public $form_template = array(
        'mailaddress' => array(
            'name'      => 'メールアドレス',
            'required'  => true,
            'max'       => 255,
            'filter'    => FILTER_HW,
            'custom'    => 'checkMailaddress',
            'form_type' => FORM_TYPE_TEXT,
            'type'      => VAR_TYPE_STRING,
        ),
        'password' => array(
            'name'      => 'パスワード',
            'required'  => true,
            'max'       => 255,
            'filter'    => FILTER_HW,
            'form_type' => FORM_TYPE_TEXT,
            'type'      => VAR_TYPE_STRING,
        ),
    );
}

子クラスで

Child_ActionForm.php
class Child_ActionForm extends Sample_ActionForm
{
     public $form_template = array(
        'mailaddress' => array(
            'required'  => false,
        ),
    );
}

このようなことをしてしまうと、
子クラスが親クラスの$form_templateを完全につぶしてしまうので、
親クラスの定義は一切引き継がれません。

この状態で孫クラスで、

Hoge_ActionForm.php
class Hoge_ActionForm extends Child_ActionForm
{
     public $form = array(
        'mailaddress' => array(),
        'password' => array(
    );
}

と書いた場合、孫クラスはpasswordの定義を再利用することはできません。(mailaddressについてもほぼ何も定義してないも同然になってしまいます)

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?