Blogger Information
Blog 64
fans 6
comment 2
visits 83017
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php trait使用详解
王娇
Original
1084 people have browsed it

学习总结

  • 一个类中可以引入多个trait,中间用逗号隔开
  • 如果使用trait后,出现方法命名冲突,可以使用insteadof关键字进行替代,或者使用as关键字起个别名
  • 可以在trait中实现接口的方法,然后在工作类中使用trait
  • 幸运大转盘实例

1.类中引入多个trait后,方法命名冲突的解决办法

  1. <?php
  2. //一个类中可以引入多个trait
  3. //trait中的方法如果重名
  4. //1.可以使用insteadof关键字进行替代
  5. //2.可以使用as关键字起一个别名,还可以修改方法的访问限制
  6. trait tQhdx
  7. {
  8. public function printSDCInfo($school = '清华大学',$department = '自动化系',$classes = '05-01')
  9. {
  10. echo '班名:',$classes,'<br>';
  11. echo '系名:',$department,'<br>';
  12. echo '校名:',$school,'<br>';
  13. echo '<hr>';
  14. }
  15. }
  16. trait tRmdx
  17. {
  18. public function printSDCInfo($school = '人民大学',$department = '工商管理',$classes = '08-02')
  19. {
  20. echo '班名:',$classes,'<br>';
  21. echo '系名:',$department,'<br>';
  22. echo '校名:',$school,'<br>';
  23. echo '<hr>';
  24. }
  25. }
  26. trait tSchool
  27. {
  28. use tQhdx,tRmdx{
  29. //如果访问printSDCInfo方法,则用清华大学替代人民大学
  30. tQhdx::printSDCInfo insteadOf tRmdx;
  31. //给清华大学中的printSDCInfo方法起别名为printQhdx;
  32. tQhdx::printSDCInfo as protected printQhdx;
  33. //给人民大学中的printSDCInfo方法起别名为printRmdx,并且修改方法的访问限制为受保护的;
  34. tRmdx::printSDCInfo as protected printRmdx;
  35. }
  36. }
  37. class StuInfo
  38. {
  39. use tSchool;
  40. public function printStuInfo($name = 'angle',$age = 32,$sex = '女',$school = '清华大学')
  41. {
  42. echo '姓名:',$name,'<br>';
  43. echo '年龄:',$age,'<br>';
  44. echo '性别:',$sex,'<br>';
  45. if ($school == '清华大学'):
  46. $this ->printQhdx();
  47. else:
  48. $this ->printRmdx();
  49. endif;
  50. }
  51. }
  52. $stu1 = new StuInfo;
  53. $stu1 ->printStuInfo();
  54. $stu2 = new StuInfo;
  55. $stu2 ->printStuInfo('hugn',31,'男','人民大学');
  56. ?>
  • 运行效果图

2.trait中可以实现接口中的方法,而不一定在工作类中实现

  1. <?php
  2. interface iSchool
  3. {
  4. public function printSDCInfo($school = '清华大学',$department = '自动化系',$classes = '05-01');
  5. }
  6. trait tStuInfo
  7. {
  8. //trait中实现接口的方法,可以使工作类中的代码更加简洁,易于维护
  9. public function printSDCInfo($school = '清华大学',$department = '自动化系',$classes = '05-01')
  10. {
  11. echo '班名:',$classes,'<br>';
  12. echo '系名:',$department,'<br>';
  13. echo '校名:',$school,'<br>';
  14. echo '<hr>';
  15. }
  16. public function printStuInfo($name = 'angle',$age = 32,$sex = '女',$school = '清华大学')
  17. {
  18. echo '姓名:',$name,'<br>';
  19. echo '年龄:',$age,'<br>';
  20. echo '性别:',$sex,'<br>';
  21. if ($school == '清华大学'):
  22. $this ->printSDCInfo();
  23. else:
  24. $this ->printSDCInfo('人民大学','工商管理系','08-02');
  25. endif;
  26. }
  27. }
  28. //接口中的方法可以在trait中实现
  29. class StuInfo implements iSchool
  30. {
  31. use tStuInfo;
  32. }
  33. $stu1 = new StuInfo;
  34. $stu1 ->printStuInfo();
  35. $stu2 = new StuInfo;
  36. $stu2 -> printStuInfo('hugn',31,'男','人民大学');
  37. ?>
  • 运行效果图

3.幸运大转盘实例

  1. <?php
  2. //当点击开始按钮时,每个奖品框的背景依次变为白色
  3. //在0-60中取一个随机数做为抽奖的时间(奖品框背景依次变为白色的时间),时间到后,哪个奖品的背景为白色,就选中哪个奖品
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <title>幸运大转盘</title>
  11. </head>
  12. <style>
  13. * {
  14. padding: 0px;
  15. margin: 0px;
  16. box-sizing: border-box;
  17. }
  18. .container {
  19. width: 500px;
  20. height: 800px;
  21. margin: auto;
  22. display: flex;
  23. flex-flow: column nowrap;
  24. justify-content: center;
  25. align-items: center;
  26. }
  27. .titleImage {
  28. width: 300px;
  29. height: 180px;
  30. margin-bottom: 15px;
  31. }
  32. .items {
  33. height: 500px;
  34. display: grid;
  35. grid-template-rows: repeat(3, 100px);
  36. grid-template-columns: repeat(3, 150px);
  37. grid-auto-flow: row;
  38. gap: 5px;
  39. justify-content: center;
  40. align-items: center;
  41. }
  42. .items>div {
  43. width: 100%;
  44. height: 100%;
  45. background-color: lightgreen;
  46. }
  47. .items>div:nth-of-type(5) {
  48. width: auto;
  49. height: auto;
  50. background-color: white;
  51. box-shadow: none;
  52. }
  53. .items>div {
  54. display: flex;
  55. justify-content: center;
  56. align-items: center;
  57. border-radius: 30%;
  58. box-shadow: 2px 2px 2px #555;
  59. }
  60. span {
  61. letter-spacing: 2px;
  62. color: blueviolet;
  63. text-shadow: 0.5px 0.5px 0.5px #555;
  64. }
  65. img {
  66. width: 100%;
  67. height: 100%;
  68. }
  69. </style>
  70. <body>
  71. <div class="container">
  72. <div class="titleImage"><img src="luck.jpg" alt=""></div>
  73. <div class="items">
  74. <div><span>50银铃铛</span></div>
  75. <div><span>10金铃铛</span></div>
  76. <div><span>10元购书卷</span></div>
  77. <div><span>谢谢参与</span></div>
  78. <div><a href=""><img src="start.gif" alt=""></a></div>
  79. <div><span>300银铃铛</span></div>
  80. <div><span>再来一次</span></div>
  81. <div><span>电子书一本</span></div>
  82. <div><span>悦读卡1天</span></div>
  83. </div>
  84. </div>
  85. </body>
  86. </html>
  • 运行效果图
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:很有创意
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post