Promena prava pristupa metodima trejta u OOP u PHP-u

Unutar trejta se može koristiti bilo koji modifikator pristupa za metode (to jest public, private ili protected). Međutim, po potrebi, u samoj klasi se ovaj modifikator može promeniti na drugi. Za ovo u telu use posle ključne reči as treba navesti novi modifikator.

Pogledajmo na primeru. Neka postoji sledeći trejt sa privatnom metodom:

php<?php
  trait TestTrait
  {
    private function method()
    {
      return '!!!';
    }
  }
?>

Povežimo naš trejt sa klasom:

php<?php
  class Test
  {
    use TestTrait;
  }
?>

Promenimo u klasi metod u javni:

php<?php
  class Test
  {
    use TestTrait {
      TestTrait::method as public;
    }
  }
?>

Proverimo rad javnog metoda spolja klase:

php<?php
  $test = new Test;
  echo $test->method(); // ispisaće '!!!'
?>