Skip to content

Instantly share code, notes, and snippets.

@cviebrock
Created March 9, 2012 16:21
Show Gist options
  • Select an option

  • Save cviebrock/2007340 to your computer and use it in GitHub Desktop.

Select an option

Save cviebrock/2007340 to your computer and use it in GitHub Desktop.
Data Factory
<?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