Treyt metodlarina icazelerin deyisdirilmesi OOP-de PHP-de

Treyin daxilinde metodlar ucun istənilan icaze modifikatorundan istifade etmek olar (yəni public, private ve ya protected). Lakin zəruret olduqda, klasin ozunde bu modifikatoru basqa birine deyismek olar. Bunun ucun use bodhisində açar soz as-dan sonra yeni icaze modifikatoru gosterilmelidir.

Nuemune uzerinde baxaq. Tutaq ki, asagidaki xususi metodu olan treytimiz var:

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

Treytti klasa qoshaq:

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

Klasdə metodu ictimai (public) edək:

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

Klasdan kenar ictimai metodun işini yoxlayaq:

php<?php
  $test = new Test;
  echo $test->method(); // '!!!' cap edecek
?>