Skip to content

Instantly share code, notes, and snippets.

@fumikito
Created December 8, 2022 04:54
Show Gist options
  • Save fumikito/21d76ac94e59fc51c428c2d65125d42c to your computer and use it in GitHub Desktop.
Save fumikito/21d76ac94e59fc51c428c2d65125d42c to your computer and use it in GitHub Desktop.

Revisions

  1. fumikito created this gist Dec 8, 2022.
    53 changes: 53 additions & 0 deletions trait-member-property.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    <?php

    trait BaseTrait {

    private $counter = 0;

    private $greeting = 'Hello World!';


    public function say_hello() {
    $this->counter++;
    echo $this->greeting . PHP_EOL;
    }

    public function greet_count() {
    printf( 'Greeted %d times' . PHP_EOL, $this->counter );
    }
    }

    trait John {

    use BaseTrait;

    public function john() {
    echo 'Hi, john. ';
    $this->say_hello();
    }
    }

    trait Lisa {
    use BaseTrait;
    public function lisa() {
    echo 'Hi, Lisa! ';
    $this->say_hello();
    }
    }


    class Me {

    use John, Lisa;

    public function greet() {
    $this->john();
    $this->lisa();
    $this->greet_count();
    }
    }

    $me = new Me();

    $me->greet();
    // ここで$countは2になっているべき