1 | <?php |
||
17 | class Guard implements GuardContract { |
||
18 | /** |
||
19 | * Default options. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $defaults = array( |
||
24 | 'rule' => 'public', |
||
25 | 'callback' => null, |
||
26 | ); |
||
27 | |||
28 | /** |
||
29 | * Guard options. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $options; |
||
34 | |||
35 | /** |
||
36 | * Instantiate a new Guard with provided options. |
||
37 | * |
||
38 | * @param array $options |
||
39 | */ |
||
40 | 21 | public function __construct( array $options = array() ) { |
|
43 | |||
44 | /** |
||
45 | * Validates whether the current user is authorized. |
||
46 | * |
||
47 | * @return true|WP_Error |
||
48 | */ |
||
49 | 21 | public function authorized() { |
|
69 | |||
70 | /** |
||
71 | * Checks whether the current user can edit other's posts. |
||
72 | * |
||
73 | * @return bool|WP_Error |
||
74 | */ |
||
75 | 6 | protected function can_edit_others_posts() { |
|
78 | |||
79 | /** |
||
80 | * Checks whether the user is currently logged in. |
||
81 | * |
||
82 | * @return bool|WP_Error |
||
83 | */ |
||
84 | 6 | protected function user_logged_in() { |
|
87 | |||
88 | /** |
||
89 | * Checks whether the user can manage the site options. |
||
90 | * |
||
91 | * @return bool|WP_Error |
||
92 | */ |
||
93 | protected function can_manage_options() { |
||
96 | |||
97 | /** |
||
98 | * Sets the default params for the Guard options. |
||
99 | * |
||
100 | * @param array $options |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | 21 | protected function set_defaults( $options ) { |
|
108 | |||
109 | /** |
||
110 | * Create an unauthorized error object. |
||
111 | * |
||
112 | * @return WP_Error |
||
113 | */ |
||
114 | 6 | protected function make_error() { |
|
117 | } |
||
118 |
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.