Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trait でクラス定数(っぽいもの)を追加する仕組み #2437

Merged
merged 2 commits into from
Aug 2, 2017

Conversation

nanasess
Copy link
Contributor

@nanasess nanasess commented Jul 27, 2017

概要(Overview・Refs Issue)

現在、一部のマスタデータにクラス定数が定義されており、簡潔に ID 値を取得できる。
しかし、カスタマイズ時にマスタ値を追加した場合、 Entity拡張機構を使用して、クラス定数を追加できないため、不便。

方針(Policy)

  • trait で追加可能な、クラス定数と同等の仕組みを実装する
  • 定義した値を変更できないようにする
  • 既存のクラス定数も同じように使用できるようにする

こんな感じに使用します

echo CustomerStatus::ACTIVE();
// => 2

従来のクラス定数にも同じように使用できます

echo CustomerStatus::ACTIVE;
// => 2

trait を追加することで、定義を増やすことができます。

trait CustomerStatusTrait
{
    private static $LEAVE = 3;
}

echo CustomerStatus::LEAVE();
// => 3

値を変更しようとすると InvalidArgumentException になります

$obj = new CustomerStatus();
$obj->LEAVE = 4;
// => InvalidArgumentException

実装に関する補足(Appendix)

$obj = new static(); // segmentation fault になる
return $obj->$name;

@chihiro-adachi chihiro-adachi added this to the 3.1.0 milestone Aug 2, 2017
@Yangsin Yangsin merged commit 4dd1493 into EC-CUBE:experimental/3.1 Aug 2, 2017
@nanasess nanasess deleted the enum-like-properties branch February 13, 2018 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants