<?php class Sport { public function whatslike() { return '这个运动不错'; } } class Test { public function showme(Sport $sport) { return '我会射箭'.$sport->whatslike(); } } $sport = new Sport (); $result = new Test (); echo $result->showme($sport); ?>
/**
* 实现依赖注入的方式:
* 1.构造方法中实现
* 2.普通方式中实现 ( 本例中使用的是这种方法)
*/