I keep repeating myself, shaking with breath, when will I be able to stand up?

autoload
Release: 2023-04-09 19:28:02
Original
2176 people have browsed it

How can some members of a class be used by other classes? If inheritance is used, the inheritance chain is too long. For convenience, PHP provides the code reuse technology trait.

1. Definition : Trait is a code reuse mechanism prepared for a single inheritance language similar to PHP. Traits can make single-inheritance languages ​​get rid of the embarrassing situation of having to inherit for reuse, making object-oriented become more pure.

2. Basic syntax:

Trait is a keyword similar to class.

<?php
     trait Eat{
        public $a=10;      //trait内允许有类的成员属性(包括静态属性),成员方法(包括静态方法)
        public static $b=666;
        //const c=3.14;       //trait内不允许有常量
        protected $e;	//允许定义,但是实际不用
        private $f;
        public function getA()
        {
               echo $this->a,"<br>";
        }
        public static function getB()
        {
               echo self::$b,"<br>";
        }
    }
?>
Copy after login

trait is used to achieve code reuse and cannot be instantiated or inherited (because it is not a class at all).

<?php
   trait Eat{}
  // $a=new Eat;//报错
  //calss A extends Eat{}//报错
?>
Copy after login

3. The use of traits

A trait is just a collection of code, which must be referenced using use.

<?php
    
    trait Eat{
        public $a=10;
        public static $b=666;
        //const c=3.14;
        public function getA()
        {
               echo $this->a,"<br>";
        }
        public static function getB()
        {
               echo self::$b,"<br>";
        }
    }
    
    class A{
        use Eat;


        public function getC()
        {
            echo $this->a,"<br>";
        }
    }

    $boy=new A();
    $boy->getC();
    $boy->getA();
    $boy->getB();
?>
Copy after login

A class can use multiple traits.

<?php
   trait A1{
   }
   trait A2{
   }
   class People{
     use A1,A2;
   }
?>
Copy after login

4. Problems with using traits

a. If multiple traits introduced at the same time have attributes with the same name, conflicts will occur.

<?php
    trait A1{
       public $a=11;
    }
    trait A2{
       public $a=22;
    }

    class A3{
        use A1,A2;//不允许同名属性
        public function geta(){
            echo $this->a;
        }
    }

    $example=new A3();
  

?>
Copy after login

b. If there are methods with the same name in multiple traits introduced at the same time, conflicts will occur. There are two solutions such as A3 and A4.

<?php
    trait A1{
       public $a=11;
       public function eat(){
           echo "A1中eat()方法","<br>";
       }
    }
    trait A2{
       public $b=22;
       public function eat(){
        echo "A2中eat()方法","<br>";
      }
    }

    class A3{
        use A1,A2{
          A1::eat insteadOf A2; //A1中的eat替代A2中的eat
        }
    }
    class A4{
        use A1,A2{
            A1::eat insteadOf A2;//A1中的eat替代A2中的eat
            A2::eat as eat2;     //A2中的eat取别名eat2
          }
    }

    $example=new A3();
    $example->eat();
    $example=new A4();
    $example->eat2();
?>
Copy after login

c. Same name coverage problem: If there is a member with the same name as the introduced trait in the class, it will be handled differently:

  • Attribute: Duplicate names are not allowed, that is, class It is not allowed to define member properties with the same name as those in the trait (the same applies to static properties).

  • Method: Class override trait.

d. Inheritance coverage problem: If a class uses trait, it also inherits from the parent class, and the trait has a method with the same name as the parent class, Then trait will overwrite the method with the same name of the parent class; if you want to access the method of the parent class, you can use the parent keyword in the trait method with the same name to access the method with the same name of the parent class.

<?php
    trait Eat{
    public function eat(){
        echo &#39;Eat::eat&#39;;
    }
}
    class Human{
    public function eat(){
        echo &#39;Human::eat&#39;;
    }
}
//子类继承父类同时使用trait
class Man extends Human{
    use Eat;
}

$m = new Man();
$m->eat();
?>
Copy after login

e.trait itself cannot be accessed. It is only used to provide code reuse for other classes. Therefore, classes are allowed to use higher access control when using traits: in as After that, use the target access modifier qualifier.

<?php
trait Eat{
    private function show(){
        echo &#39;eat&#39;;
    }
}
class Human{
    use Eat{
        show as public eshow;				
        //注意:as是用来设定别名的,虽然没有同名show,但是系统认为show已经存在,所以必须别名,权限的更改的方法不是
        //本尊
    }
}
$h = new Human();
$h->eshow();								//eat
?>
Copy after login

You can use abstract methods in f.trait, and the using class must implement the corresponding abstract method: The using class must either be an abstract class, or it must implement an abstract method.

<?php
   trait Eat{
     public function eat();		//抽象方法
   }
   abstract class Human{			
    use Eat;					//抽象类:可以不实现抽象方法
   }

   class Animal{
    use Eat;
    public function eat(){		//具体类:实现抽象方法
        echo &#39;Animal::eat&#39;;	
    }		
}
?>
Copy after login

Recommended: php tutorial,php video tutorial

The above is the detailed content of I keep repeating myself, shaking with breath, when will I be able to stand up?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!