Here is the functi开发者_JAVA技巧on I got:
function update_user_information($aUserId, $aName, $aPassword)
If I would like to update one more attribute, I need to do something like this:
function update_user_information($aUserId, $aName, $aPassword, $aEmail)
If the user table keep extend, my function will always change, how to make it simpler for future update? Thank you.
I would create a Bean in a java style.
this way I think is more appropriate way to keep the logic simple.
see bellow how to use it
class user{
private $name;
private $userName;
public function update(){
///business logic
}
public function getName(){
return $this->name;
}
public function getUserName(){
return $this->userName;
}
public function setName($name){
$this->name = $name;
}
public function getUserName($userName){
$this->userName = $userName;
}
}
//this is how you user it.
$user = new User();
$user->setName('test');
$user->update();
this way you can add more functionality like insert(), clone(), sendMailToUser() without the need to handle the parameters again and again in each function.
Consider creating a User class, and then passing in that as the parameter.
Passing in an array of parameters makes it very difficult for the next programmer that comes along to determine what parameters are expected.
use an array of strings as parameter:
function update_user_information(array $parameters)
ur use a class
The easiest would be to pass an array (like was already mentioned a few times now)
update_user(array('username' => 'bar'));
but that has the disadvantage that your API no longer communicates what values are acceptable to update a user. You'd have to put that into your API documentation. And of course, you'd have to update that each time you allow a new field to be set. You will also want to check inside the function if all required values have been passed.
If you are not fixed on using procedural, you could also use a Builder pattern
class UserBuilder implements ArrayAccess
{
protected $data;
public function setUsername($username) {
$this->data['username'] = $username;
return $this;
}
public function offsetGet($offset) {
return $this->data[$offset];
}
public function getUser() {
$user = new User; // or whatever you use to create new users
$user->update($this);
return $user;
}
…
This will allow you to create completely new Users by doing
$userBuilder = new UserBuilder;
$userBuilder->setUsername('foo');
$user = $userBuilder->getUser();
as well as updating existing users:
update_user($userBuilder
->setUsername('bar')
->setOtherProperty('baz')
…
);
The API of the Builder will tell a developer which fields s/he can set and you have the added benefit of being able to collect values incrementally. Using a builder will also capsule the responsibility of creating objects (some fields might require more object to be created), which is better than putting new
keywords all over the place.
If you have a User class, it doesnt need to expose any of its internals and can focus to be an Information Expert and only expose those methods that operate on the object as a whole.
class User
{
public function update($userBuilder)
{
$this->username = $userBuilder['username'];
…
}
}
Note that the UserBuilder implements ArrayAccess so whether you pass an array or a builder instance doesnt matter to the User. This will work too:
$user->update(array('username' => 'bar'));
The only drawback is that you'll still need to add new accessors to the builder whenever you need additional fields, but that's a small price to pay for the flexibility you achieve this way.
Create update and user class.
$ins->update->user(array('username' => 'Thomas'));
Ofcourse you can make not just user class.
Or you can pass user object.
$user->username = 'Thomas', $ins->update($user);
I do it "jQuery" style:
$some_helper = new some_helper();
$a_user = new stdClass();
$a_user->userID = 1;
$some_helper->update_something($a_user);
abstract class some_helper_class() {
function extend($defaults, $settings) {
foreach($defaults as $key => $value) {
if (!isset($settings->$key)) {
$settings->$key = $value;
}
}
return $settings;
}
}
class some_helper extends some_helper_class{
function update_something($data) {
$defaults = new stdClass();
// $defaults->userID = 1; // required, so we do not set it now
$defaults->name = 'my name';
$defaults->email = 'a@b.com';
$defaults->password = 'secret';
$this->extend($defaults, $data);
$defaults->userID = 1;
// validation is important always
if (count($defaults) != count($data)) {
// Danger ! required values not present.
} else {
// all is well - do your stuff
}
}
}
精彩评论