LoginSignup
1
1

More than 3 years have passed since last update.

PHP トレイトとは

Last updated at Posted at 2020-07-14

トレイト

class内の中にclassを追加してそのメゾットなどを再利用することができる

サンプルコード.

sample.php
<?php

class Car
{
 public function run()
 {
 echo "running by Car\n";
 }

 public function horn()
 {
 echo "beeeep!! by Car\n";
 }
}

trait Honda
{
 public function run()
 {
 parent::run();
 parent::horn();
 echo "running!! by Honda\n";
 }
}

class Machine extends Car
{
 use Honda;
}

$machine = new Machine();
$machine->run();

# running by Car
# beeeep!! by Car
# running!! by Honda



参考

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