PHP OOPda Treytlar bilan ishlashda metodlarning ustuvorligi

Agar klassda va treytda bir xil nomli metod mavjud bo'lsa, unda klass metodı treyt metodini bekor qiladi:

php<?php
  trait TestTrait
  {
    // Method nomli metod:
    public function method()
    {
      return 'trait';
    }
  }
  
  class TestClass
  {
    use TestTrait;
    
    // Xuddi shu nomdagi metod:
    public function method()
    {
      return 'test';
    }
  }
  
  $test = new TestClass;
  echo $test->method(); // 'test' chiqadi - klassning o'z metodı ishladi
?>

Agar klassning o'zida bunday metod bo'lmasa, lekin treyt metodlari va ota-klass metodlari o'rtasida nomlar ziddiyati mavjud bo'lsa, unda treyt metodlari ustunlik qiladi:

php<?php
  trait TestTrait
  {
    // Method nomli metod:
    public function method()
    {
      return 'trait';
    }
  }
  
  // Ota-klass:
  class ParentClass
  {
    // Method nomli metod:
    public function method()
    {
      return 'parent';
    }
  }
  
  // Klass method metodini otadan meros qilib oladi:
  class TestClass extends ParentClass
  {
    use TestTrait;
  }
  
  $test = new TestClass;
  echo $test->method(); // 'trait' chiqadi, chunki treyt ustunlik qiladi
?>