Create a trait and use it in a class:
<?php
trait message1 {
public function msg1() {
echo "OOP is fun! ";
}
}
class Welcome {
use message1;
}
$obj = new Welcome();
$obj->msg1();
?>
Try it Yourself »
The use
keyword has two purposes: it tells a class to inherit a trait and it gives an alias to a namespace.
Read more about traits in our PHP OOP - Traits Tutorial.
Read more about namespaces our PHP Namespaces Tutorial.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!