1
0

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 1 year has passed since last update.

PHP traitの継承的な書き方

Last updated at Posted at 2022-03-08

traitの継承的なやり方について、適切な記事がなかったので自分なりのまとめ

php version

bash
# php -v
PHP 7.1.33 (cli) (built: Oct 31 2019 17:37:57) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans

traitの継承的な書き方

test1.php
<?php

namespace Traits;

trait test1
{
    /**
     * @return string
     */
    public function execute(): string {
        return 'a';
    }
}
test2.php
<?php

namespace Traits;

trait test2
{
    use \Traits\test1:

    /**
     * @return string
     */
    public function execute(): string {
        return 'b';
    }
}

継承したいtrait内で
対象のtraitをuseして関数を上書きしてあげる

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?