ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# trait ## **Trait** > 代码复用的方法 优先顺序是当前类中的方法会覆盖 trait 方法,而 trait 方法又覆盖了基类中的方法。 ``` <pre class="calibre10">``` class <span class="token4">Base</span> <span class="token3">{</span> public <span class="token5">function</span> <span class="token4">sayHello</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> echo <span class="token2">'Hello '</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> trait <span class="token4">SayWorld</span> <span class="token3">{</span> public <span class="token5">function</span> <span class="token4">sayHello</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> parent<span class="token3">:</span><span class="token3">:</span><span class="token4">sayHello</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> echo <span class="token2">'World!'</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> class <span class="token4">MyHelloWorld</span> extends <span class="token4">Base</span> <span class="token3">{</span> use SayWorld<span class="token3">;</span> <span class="token3">}</span> $o <span class="token1">=</span> <span class="token5">new</span> <span class="token4">MyHelloWorld</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $o<span class="token1">-</span><span class="token1">></span><span class="token4">sayHello</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` 输出: Hello World!