1 | <?php |
||
32 | abstract class GenericServlet implements ServletInterface |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * The servlet configuration. |
||
37 | * |
||
38 | * @var \AppserverIo\Psr\Servlet\ServletConfigInterface |
||
39 | */ |
||
40 | protected $servletConfig; |
||
41 | |||
42 | /** |
||
43 | * Information about the servlet, such as author, version, and copyright. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $servletInfo = ''; |
||
48 | |||
49 | /** |
||
50 | * Initializes the servlet with the passed configuration. |
||
51 | * |
||
52 | * @param \AppserverIo\Psr\Servlet\ServletConfigInterface $servletConfig The configuration to initialize the servlet with |
||
53 | * |
||
54 | * @return void |
||
55 | */ |
||
56 | public function init(ServletConfigInterface $servletConfig) |
||
60 | |||
61 | /** |
||
62 | * Returns the servlet configuration. |
||
63 | * |
||
64 | * @return \AppserverIo\Psr\Servlet\ServletConfigInterface The servlet configuration |
||
65 | */ |
||
66 | public function getServletConfig() |
||
70 | |||
71 | /** |
||
72 | * Returns information about the servlet, such as author, version, and copyright. By default, this method |
||
73 | * returns an empty string. You have to override this method to have it return a meaningful value. |
||
74 | * |
||
75 | * @return string The servlet information |
||
76 | */ |
||
77 | public function getServletInfo() |
||
81 | |||
82 | /** |
||
83 | * Returns the servlet context instance. |
||
84 | * |
||
85 | * @return \AppserverIo\Psr\Servlet\ServletContextInterface The servlet context instance |
||
86 | */ |
||
87 | public function getServletContext() |
||
91 | |||
92 | /** |
||
93 | * Returns the servlets name from the web.xml configuration file. |
||
94 | * |
||
95 | * @return string The servlet name |
||
96 | */ |
||
97 | public function getServletName() |
||
101 | |||
102 | /** |
||
103 | * Returns the init parameter with the passed name. |
||
104 | * |
||
105 | * @param string $name Name of the init parameter to return |
||
106 | * |
||
107 | * @return null|string |
||
108 | */ |
||
109 | public function getInitParameter($name) |
||
113 | } |
||
114 |
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.