Created
March 9, 2012 16:21
-
-
Save cviebrock/2007340 to your computer and use it in GitHub Desktop.
Data Factory
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 | |
| class Data { | |
| public static function factory() | |
| { | |
| $args = func_get_args(); | |
| if (count($args)==0) { | |
| // no args? must just want a copy of itself (not sure why) | |
| return new static; | |
| } | |
| // okay, we're trying to make something. get the class type | |
| $classname = 'Data_'.$args[0]; | |
| if (count($args)==1) { | |
| // we're just creating a new class | |
| return new $classname; | |
| } | |
| // okay, so we're also passing data ... let's check it | |
| // if it's empty, return a Data_Null model instead of what they | |
| // really asked for | |
| $attributes = $args[1]; | |
| // todo: check if $data isn't an array? | |
| if (count(array_filter($attributes)) { | |
| return new $classname($attributes); | |
| } | |
| return new Data_Null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment