| 1 | <?php |
||
| 4 | class EE_Boolean_Field extends EE_Integer_Field |
||
| 5 | { |
||
| 6 | /** |
||
| 7 | * @param string $table_column |
||
| 8 | * @param string $nicename |
||
| 9 | * @param bool $nullable |
||
| 10 | * @param null $default_value |
||
| 11 | */ |
||
| 12 | public function __construct($table_column, $nicename, $nullable, $default_value = null) |
||
| 17 | |||
| 18 | public function prepare_for_set($value_inputted_for_field_on_model_object) |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Make sure we're returning booleans |
||
| 29 | * |
||
| 30 | * @param string $value_inputted_for_field_on_model_object |
||
| 31 | * @return boolean |
||
| 32 | */ |
||
| 33 | public function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Gets a nice Yes/No value for this field |
||
| 40 | * |
||
| 41 | * @param boolean $value_on_field_to_be_outputted |
||
| 42 | * @return string Yes or No |
||
| 43 | */ |
||
| 44 | public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
||
| 52 | } |
||
| 53 |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.