Instantiating Class with Arguments

(That sounds funny. I think OOP can make anyone want to ARGUE!!!) :smiley:

So I’m trying to learn PHP OOP.

How can I create a new object and assign values to its properties as I’m instantiating the class?

For instance - pun intended! - I have a class User.

When I create an object, I am hoping to have…

$newUser->firstName = ‘Tom’

$newUser->lastName = ‘Tees’

Make sense?!

TomTees


class User {
  private $firstName;
  private $lastName;

  public function __construct($firstname, $lastname) {
    $this->firstName = $firstname;
    $this->lastName = $lastname;
  }

  /**
  * Mutators / Setters
  */
  public function setFirstName($firstname) {
    $this->firstName = $firstname;
  }

  public function setLastName($lastname) {
    $this->lastName = $lastname;
  }

  /**
  * Accessors / Getters
  */
  public function getFirstName() {
    return $this->firstName;
  }

  public function getLastName() {
    return $his->lastName;
  }
}

When you instantiate an object then you would do:


$user = new User('Tom', 'Tees');

echo $user->getFirstName() . ' ' . $user->getLastName(); //Tom Tees
$user->setFirstName('Proxi');

echo $user->getFirstName() . ' ' . $user->getLastName(); //Proxi Tees

You might want to check out this article then…

TomTees

Proxi,

Thanks for the help! You made that look easy! :blush:

BTW, I have heard several people say that Setters and Getters are evil because they break Encapsulation.

If that is true, then what is the workaround?

TomTees

It doesn’t break encapsulation. When I modify one user object, it doesn’t affect the other:


$userA = new User('John', 'Doe');
$userB = new User('Jane', 'Doe');

$userB->setFirstName('Jason');

echo $userA->getFirstName() . ' ' . $userA->getLastName(); //John Doe

echo $userB->getFirstName() . ' ' . $userB->getLastName(); //Jason Doe

You should only create setters and getters where needed.

Most of the time you can get away with not creating mutators as you can do that within the class itself.


$user = new User('John', 'Doe');

echo $user->getFirstName(); // output: John

//user want's to change their information. Normally you get the values from a GET/POST request.
//but we'll just hard code it as an example
$user->update(array(
  'firstName' => 'Jane'
));

echo $user->getFirstName(); //ouput: Jane

No where did you see a mutator method. It all happened behind the scenes (aka the update() method).

That was an interesting article.

The main thing to take away from that article is that class design should be task oriented. So if you would like to follow the methodology expressed in the article the user should provide methods to carry out full procedures than just be a data storage facility. The methodology expressed by the author is a very push oriented, so I’m so sure a lot of people here would disagree. I myself think the author brings up some good points, but to go as far to say getters and setters are evil, isn’t the intention of the article. As the author put it design is a series of decisions that have negative and positive consequences. You should always make decisions with that mind, not completely change your own methodology or never use getters and setters. Though if you had reason to go the route your going than an interface may be order. None the less, its hard to say the best route with such a undeveloped, generic class without knowing all the details about the application. Design in any respect is all about Gestalt. So not knowing the all things that have been created up to this point, intent, plans, etc makes it impossible to say what direction is best in terms of your design.