1 | <?php |
||
10 | class MeetingsController extends Controller |
||
11 | { |
||
12 | 15 | public function __construct() |
|
18 | |||
19 | /** |
||
20 | * Display a listing of the resource. |
||
21 | * |
||
22 | * @param Request $request needed for get query to get only current planed meetings |
||
23 | * @return \Illuminate\Http\Response |
||
24 | */ |
||
25 | 6 | public function index(Request $request) |
|
37 | |||
38 | /** |
||
39 | * Display the specified resource. |
||
40 | * |
||
41 | * @param int $id |
||
42 | * @return \Illuminate\Http\Response |
||
43 | */ |
||
44 | 6 | public function show($id) |
|
50 | |||
51 | /** |
||
52 | * Store a newly created resource in storage. |
||
53 | * |
||
54 | * @param int $groupId |
||
55 | * @param int $meetingId |
||
56 | * @return static |
||
57 | */ |
||
58 | public function showImage($groupId, $meetingId) |
||
70 | |||
71 | private static function getImg(Meeting $meeting) |
||
78 | } |
||
79 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.