|
Posted by amygdala on 04/15/07 20:41
"Toby A Inkster" <usenet200703@tobyinkster.co.uk> schreef in bericht
news:rrr2f4-4kp.ln1@ophelia.g5n.co.uk...
> amygdala wrote:
>
>> I'm a little confused about the 'factory design pattern' though. Let me
>> see
>> if I understand this correctly. Does the factory design pattern in this
>> case
>> refer to the fact that you have different interfaces in one Class (in
>> this
>> case class User) to create the object, depending on the purpose it
>> serves.
>
> Yep, precisely. You create a static method to produce an object which
> would normally go further than the plain constructor function in terms of
> setting object properties.
>
> On some occasions you may even want to make the main constructor method
> for the class private instead of public to force all objects to be created
> via the factory.
I think I am starting to get the hang of this factory design pattern. My
class User up until now looks something like (stripped version, and needs
custom error handling, etc.):
<?php
require_once( 'Database.class.php' );
class User
{
// database handler values
private static $dbh;
private static $dbt = DB_TABLE_USER;
// table columns
private $id;
private $userType;
private $username;
private $password;
private $email;
private $active;
private $timeCreated;
private $timeActivated;
private $timeUpdated;
public function __construct()
{
self::$dbh = Database::getInstance( DB_INTERFACE, DB_HOST, DB_SCHEMA,
DB_USERNAME, DB_PASSWORD );
}
public function getUsername()
{
return $this->username;
}
// and a bunch of other getter and setter methods
public static function getUserById( $id )
{
if( is_mysql_int( $id, true ) )
{
self::$dbh = Database::getInstance( DB_INTERFACE, DB_HOST, DB_SCHEMA,
DB_USERNAME, DB_PASSWORD );
$sql = 'SELECT * FROM ' . self::$dbt . ' WHERE id = :id LIMIT 1';
$sth = self::$dbh->prepare( $sql );
try
{
$sth->execute( array(
':id' => $id
)
);
}
catch( PDOException $e )
{
die( $e->getMessage() . ' on line: ' . __LINE__ );
}
$row = $sth->fetch( PDO::FETCH_ASSOC );
if( $row )
{
$user = new User();
foreach( $row as $column => $value )
{
$user->{ $column } = $value;
}
return $user;
}
echo 'no user found!';
return NULL;
}
echo 'invalid id!';
return NULL;
}
}
?>
A few questions though:
Since I would like to have only one database handler that can be accessed by
both the constructor and the factory method (?) getUserById() I decided to
declare $dbh static. Does this make sense? If so, it seems I'm not allowed
to declare it all in one go at the start of the class like so:
private static $dbh = Database::getInstance( DB_INTERFACE, DB_HOST,
DB_SCHEMA, DB_USERNAME, DB_PASSWORD );
Any ideas why not?
But what is of more concern for me right now is the following:
A user should be able to register and later on login to the website I'm
building. Logging in, seems pretty obvious, should be a method of the User
class, which returns a User object just like getUserById(). But would a
register method be a logical thing to put in the User class? Or would you
put that somewhere else?
Thank you once more for any insight.
Navigation:
[Reply to this message]
|