【PHP】インターフェースとトレイト
こんにちは😊Kintarouです。
現在エンジニア転職を目指してプログラミング学習中です👨🎓
夢はフリーランスエンジニアになって働く人にとって働く事が楽しくなるシステムを作ること!
と、愛する妻と海外移住すること🗽
プログラミングや読んでいる本のことなど、ブログに書いていきます!
twitter : https://twitter.com/ryosuke_angry
今回参考にさせて頂いたサイト様🙇♂️ dotinstall.com
インターフェースとは
抽象メソッドのみを持ち、継承関係を無視していくつでもクラスに実装する事ができる。実装したクラスにはインターフェースの型が継承される。
一度フォロワーを増やすfollow機能をインターフェースで実装してみます。
<?php #interface として記述する。 interface followInterface { public function follow(); } abstract class BaseUser { protected $name; public function __construct($name) { $this->name = $name; } abstract public function profile(); } class TestUser extends BaseUser implements followInterface { private $pattern; #followerを初期値0で設定し、followメソッドでfollowerを1増やす定義をする。 private $follower = 0; public function follow() { $this->follower++; } public function __construct($name, $pattern) { parent::__construct($name); $this->pattern = $pattern; } #profileメソッドでfollower数も表示させる。 public function profile() { printf('%s(%s)(%d)' . PHP_EOL, $this->name, $this->pattern, $this->follower); } } class ProductUser extends BaseUser implements followInterface { private $age; #followerを初期値0で設定し、followメソッドでfollowerを1増やす定義をする。 private $follower = 0; public function follow() { $this->follower++; } public function __construct($name, $age) { parent::__construct($name); $this->age = $age; } #profileメソッドでfollower数も表示させる。 public function profile() { printf('%s(%d)(%d)' . PHP_EOL, $this->name, $this->age, $this->follower); } } $users = []; $users[0] = new TestUser('Testarou', 'first-pattern'); $users[1] = new ProductUser('Productarou', 30); #followメソッドを実行する。 $users[0]->follow(); $users[1]->follow(); function profileBaseUser(BaseUser $user) { $user->profile(); } foreach($users as $user) { profileBaseUser($user); } #follower数が表示されています。 #=>Testarou(first-pattern)(1) #=>Productarou(30)(1)
続いて、トレイトについてです。
トレイトとは
メソッドのコードを記述し、クラスに引用する事ができる。トレイトは型ではないので継承されない。
今回は$followerとfollowメソッドの記述をfollowTraitとして記述します。
<?php #trait として記述する。 trait followTrait { private $follower = 0; public function follow() { $this->follower++; } } interface followInterface { public function follow(); } abstract class BaseUser { protected $name; public function __construct($name) { $this->name = $name; } abstract public function profile(); } class TestUser extends BaseUser implements followInterface { private $pattern; #use followTraitとして、トレイトを実装する。 use followTrait; public function __construct($name, $pattern) { parent::__construct($name); $this->pattern = $pattern; } #profileメソッドでfollower数も表示させる。 public function profile() { printf('%s(%s)(%d)' . PHP_EOL, $this->name, $this->pattern, $this->follower); } } class ProductUser extends BaseUser implements followInterface { private $age; #use followTraitとして、トレイトを実装する。 use followTrait; public function __construct($name, $age) { parent::__construct($name); $this->age = $age; } #profileメソッドでfollower数も表示させる。 public function profile() { printf('%s(%d)(%d)' . PHP_EOL, $this->name, $this->age, $this->follower); } } $users = []; $users[0] = new TestUser('Testarou', 'first-pattern'); $users[1] = new ProductUser('Productarou', 30); #followメソッドを実行する。 $users[0]->follow(); $users[1]->follow(); function profileBaseUser(BaseUser $user) { $user->profile(); } foreach($users as $user) { profileBaseUser($user); } #follower数が表示されています。 #=>Testarou(first-pattern)(1) #=>Productarou(30)(1)
以上、どなたかの参考になれば幸いです😊