views:

128

answers:

2

I have several classes that are basically interfaces to database rows. Since the class assumes that a row already exists ( __construct expects a field value ), there is a public static function that allows creation of the row and returns an instance of the class.

Here's a pseudo-code ( so there are mistakes and missing improvements in this ) example :

class fruit {

        public $id;

        public function __construct( $id ) {

                if ( ! is_numeric($id) ) {
                    throw new Exception("Id is not numeric.");
                }
                $this->id = $id;
            $sql = "SELECT * FROM Fruits WHERE id = $id";
            ...
            $this->arrFieldValues[$field] = $row[$value];
        }

    public function __get( $var ) {
        return $this->arrFieldValues[$var];
    }

    public function __set( $var, $val ) {
        $sql = "UPDATE fruits SET $var = " .  mysql_real_escape_string($val) . " WHERE id = $this->id";
    }

        public static function create( $fruit ) {

        $sql = "INSERT INTO Fruits ( fruit_name ) VALUE ( '" mysql_real_escape_string($fruit) . "' )";
        $id = mysql_insert_id();        
        $fruit = & new fruit($id);
            return $fruit;

        }
}

$obj1 = fruit::create( "apple" );
$obj2 = & new fruit( 12 );

What is this pattern called?


Edit: I changed the example to one that has more database-interface functionality. For most of the time, this kind of class would be instantiated normally, through __construct(). But sometimes when you need to create a new row first, you would call create().

+5  A: 

I think it's the Factory method pattern.

The factory method pattern is an object-oriented design pattern to implement the concept of factories.

Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The factory method design pattern handles this problem by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.

Outside the scope of design patterns, the term factory method can also refer to a method of a factory whose main purpose is creation of objects.

CD Sanchez
I don't know if it's really the factory pattern; this example can only create its own type. The factory seems to be a clearing house for making objects of all different classes. This pattern's main thrust is to provide updates and selects to the database; the `create()` method is not the main thing about it. Whereas the factory's single purpose is to create other objects.
Although in theory this example follows some logic described by the factory method pattern, of its essential roles, the factory and the product, only the former is implemented. Being the product the factory itself, the main value of this design pattern (loose coupling) is lost.
nuqqsa
Well on the wikipedia page the `Complex` example had two methods that both created objects of the same type (but they did make the constructor private), so I'm not sure whether it must always create an object of a different class. Admittedly, I'm no expert on design patterns (the opposite in fact) and Wikipedia can't always be trusted.
CD Sanchez
Under the 'Factory (software concept)' article it says that the singleton can be considered a Factory pattern. So if Singleton, which creates itself, is a factory, then this must be too. I'm choosing this answer :)
A: 

As this is related to databases, I think this is close to something that may be called Data Mapper.

If you are looking for something like this in PHP, move to Propel ORM. Look at the first example : it's almost your code !

mexique1