批改状态:合格
老师批语:
trait可以实现代码的复用,下面用实例来演示一下:
<?php
trait Course
{
public function study($course='php')
{
return '我们在php中文网学习'.$course;
}
}
class Student
{
use Course;
}
$student = new Student();
echo $student->study();点击 "运行实例" 按钮查看在线实例
运行结果:

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号