<?php
trait MyTrait
{
public function justTalk()
{
$this->speak() ;
}
}
class MyClass1{
use MyTrait ;
public function speak(){
echo "spaeak from myclass1" . PHP_EOL ;
}
}
class MyClass2{
use MyTrait ;
public function speak(){
echo "spaeak from myclass2" . PHP_EOL ;
}
}
$myclass1 = new MyClass1() ;
$myclass2 = new MyClass2() ;
$myclass1 ->justTalk() ;
$myclass2 ->justTalk() ;
以上定义了一系列内容,并加以测试。
以下是结果:
spaeak from myclass2
spaeak from myclass1