Posted by amygdala on 04/12/07 20:15
"Toby A Inkster" <usenet200703@tobyinkster.co.uk> schreef in bericht
news:jd61f4-4kp.ln1@ophelia.g5n.co.uk...
> amygdala wrote:
>
>> But the Validator class only checks each field seperately. In order to
>> keep
>> the Validator class as common as possible would you build a seperate
>> Login
>> class for instance? A class that has some custom validation methods? Or
>> perhaps just insert custom validation methods in the User class?
>
> class User
> {
> public static logon ($username, $password)
> {
> // Example query. You will want to protect against SQL
> // injection here.
> $q = "SELECT blah
> FROM blah
> WHERE username=$username
> AND password=$password";
> $result = run_query($q);
>
> if (!$result)
> return NULL;
>
> $user = new User;
> $user->load_data(/* blah */);
> return $user;
> }
> }
>
> This is an example of a User "factory". See "factory design pattern" in
> the
> PHP manual. To use it:
>
> $logged_in_user = User::logon($_POST['un'], $_POST['pw']);
> if (!$logged_in_user)
> echo "Login failed.";
>
Thanks for the response and the pointer.
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.
So I could do a:
$blank_user = new User();
or I could do a
$logged_in_user = User::logon($_POST['un'], $_POST['pw']);
which both return a User object?
[Back to original message]
|