Created
December 12, 2011 19:59
-
-
Save RalfAlbert/1468836 to your computer and use it in GitHub Desktop.
Something like MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function get_some_foo(){ | |
| return array( 'foo_one', 'foo_two', 'foo_three' ); | |
| } | |
| function get_some_bar(){ | |
| return array( 'bar_one', 'bar_two', 'bar_three' ); | |
| } | |
| function some_view( $the_array ){ | |
| $output = '<ul>'; | |
| foreach( $the_array as $element ) | |
| $output .= sprintf( '<li>%s</li>', $element ); | |
| $output .= '</ul>'; | |
| return $output; | |
| } | |
| function some_storage( $the_array ){ | |
| $filename = 'storage_view.txt'; | |
| $filehandle = fopen( $filename, 'w+' ); | |
| if( $filehandle ){ | |
| $file_output = some_view( $the_array ); | |
| fwrite( $filehandle, $file_output ); | |
| fclose( $filehandle ); | |
| // on success | |
| return true; | |
| } else { | |
| // on error | |
| return false; | |
| } | |
| } | |
| function some_controller( $get_what, $do_what ){ | |
| // check args | |
| $get_what = 'get_some_' . $get_what; | |
| if( ! in_array( $get_what, array( 'get_some_foo', 'get_some_bar' ) ) ) | |
| $get_what = 'get_some_foo'; | |
| $do_what = 'some_' . $do_what; | |
| if( ! in_array( $do_what, array( 'some_view', 'some_storage' ) ) ) | |
| $do_what = 'some_view'; | |
| // get the array | |
| $the_array = call_user_func( $get_what ); | |
| // do something with the array | |
| $output = call_user_func( $do_what, $the_array ); | |
| $success = false; | |
| // $output is not false (no error) and it is a string, we can print $output | |
| // we complete the job (creating a list) successfully, store this state in $success | |
| if( false !== $output && is_string( $output ) ){ | |
| echo $output; | |
| $success = true; | |
| } | |
| // if $output is true but not a string, the job (storing the output) was successfully done. | |
| // on error, $output is false. we can store this state (true or false) in $success | |
| else { | |
| $success = $output; | |
| } | |
| /* | |
| * do something with $success | |
| * | |
| */ | |
| var_dump( $success ); | |
| } | |
| some_controller( 'foo', 'view' ); | |
| some_controller( 'bar', 'view' ); | |
| some_controller( 'bar', 'storage' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment