LoginSignup
4
4

More than 3 years have passed since last update.

[PHP] traitを使おう

Last updated at Posted at 2014-09-30

なにか?

インスタンス化しなくても、水平方向の継承が出来る。

なぜ使うか?

クラス設計後に、機能付け足したい時とか。

サンプルコード

trait_sample.php
<?php

trait Access
{
    public function destination($to_where)
    {
        echo $to_where;
    }
}

class Navi
{
    use Access;
}

$navi = new Navi();
$navi->destination('青森'); // 青森

4
4
2

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
4
4