1 | <?php |
||
26 | class Role extends BasicEntity implements RoleInterface |
||
27 | { |
||
28 | /** |
||
29 | * Role name. |
||
30 | * |
||
31 | * @ORM\Column(name="name", type="string", length=30) |
||
32 | * @Assert\NotNull |
||
33 | * @Assert\NotBlank |
||
34 | */ |
||
35 | private $name; |
||
36 | |||
37 | /** |
||
38 | * Role identification. |
||
39 | * |
||
40 | * @ORM\Column(name="role", type="string", length=20, unique=true) |
||
41 | * @Assert\NotNull |
||
42 | * @Assert\NotBlank |
||
43 | */ |
||
44 | private $role; |
||
45 | |||
46 | /** |
||
47 | * Resources of the role and their permissions. |
||
48 | 49 | * |
|
49 | * @ORM\OneToMany(targetEntity="RoleResource", mappedBy="role", cascade={"remove"}, fetch="EAGER")) |
||
50 | 49 | **/ |
|
51 | 49 | private $resources; |
|
52 | |||
53 | /** |
||
54 | * Class constructor. |
||
55 | */ |
||
56 | public function __construct() |
||
60 | 6 | ||
61 | /** |
||
62 | * Get role object. |
||
63 | * |
||
64 | * @see RoleInterface |
||
65 | */ |
||
66 | public function getRole() |
||
70 | 6 | ||
71 | /** |
||
72 | * Gets the value of name. |
||
73 | * |
||
74 | * @return mixed |
||
75 | */ |
||
76 | public function getName() |
||
80 | 49 | ||
81 | /** |
||
82 | 49 | * Sets the value of name. |
|
83 | * |
||
84 | 49 | * @param string $name the name |
|
85 | * |
||
86 | * @return self |
||
87 | */ |
||
88 | public function setName($name) |
||
94 | 49 | ||
95 | /** |
||
96 | 49 | * Sets the value of role. |
|
97 | * |
||
98 | 49 | * @param string $role the role |
|
99 | * |
||
100 | * @return self |
||
101 | */ |
||
102 | public function setRole($role) |
||
108 | 30 | ||
109 | /** |
||
110 | 30 | * Get resource by name. |
|
111 | 30 | * |
|
112 | 30 | * @param string $name [description] |
|
113 | * |
||
114 | * @return Resource [description] |
||
115 | */ |
||
116 | 1 | public function getResource($name) |
|
126 | 2 | ||
127 | /** |
||
128 | * Gets the value of resources. |
||
129 | * |
||
130 | * @return mixed |
||
131 | */ |
||
132 | public function getResources() |
||
136 | 1 | ||
137 | /** |
||
138 | 1 | * Sets the value of resources. |
|
139 | * |
||
140 | 1 | * @param mixed $resources the resources |
|
141 | * |
||
142 | * @return self |
||
143 | */ |
||
144 | public function setResources($resources) |
||
150 | } |
||
151 |
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.