Which Of These PHP OOP Examples Is The "Common" Practice Of PHP OOP Structure?
Hey all. Looking beyond the purpose of the following two OOP examples, which is considered common or the correct structure? Or is it simply preference?
Example 1:
class names
{
private $first_name;
public function setUpperCase($first_name)
{
$this->first_name = ucfirst($first_name);
}
public function viewUpperCase()
{
echo $this->first_name;
}
}
$names = new names();
$names->setUpperCase("jimbo");
$names->viewUpperCase();
Example 2:
class names
{
public function setUpperCase($first_name)
{
$upper_first_name = ucfirst($first_name);
return $upper_first_name;
}
public function viewUpperCase($upper_first_name)
{
echo $upper_first_name;
}
}
$names = new names();
$uppercase = $names->setUpperCase("jimbo");
$names->viewUpperCase($uppercase);
Th开发者_如何学编程e first example sets the variable within the class structure. The second example sets the variable as a method argument. Both do exactly the same thing. But which is "proper"?
Thanks all. Cheers!
My proposal
class Name
{
protected $firstName;
public function __construct($firstName)
{
$this->firstName = $firstName;
}
public function getUpperCase()
{
return ucfirst($this->firstName);
}
}
$name = new Name('jimbo');
echo $name->getUpperCase();
Im going to go out on a limb and say the first example would be the "right" method as an object by definition is defined by properties. It almost seems like you have the beginnings of a user class that has a name that can have certain modifications applied, such as your uppercase method. The second example looks like it could just be a utility and I wouldnt describe it as a true object as it doesnt have any properties.
They do not do the exact same thing, and consequently it's not a matter of preference. The first example stores the uppercased filename in the object, the second does not. Which one you want / need depends on the situation.
Your first code segment is the correct one. Your examples however don't really encapsulate (excuse the pun) the real essence of OOP.
Objects have attributes and methods. The attributes store information about the object and the methods modify this information. Your first sample does this by storing the name as an attribute. Regardless of which method is called the firstname attribute persists throughout the class.
In the second example you're not declaring an attribute at all. You merely have two unrelated methods that work with their own data. Outside of the method your attribute value does not exist.
Another way to look at this is if you wanted to add a surname attribute and a getFullName() method. Example 1 would allow this quite easily.
class names
{
private $first_name;
private $surname;
public function getFullName()
{
return $this->first_name . ' ' . $this->surname;
}
public function setUpperCase($first_name)
{
$this->first_name = ucfirst($first_name);
}
public function viewUpperCase()
{
echo $this->first_name;
}
}
$names = new names();
$names->setUpperCase("jimbo");
$names->viewUpperCase();
With Example 2 this becomes contrived because you'd have to pass both names into the getFullName() method in order to get the correct return value. This defeats the purpose of creating an object which is meant to be an entity that stores all data and related operations. In Example 2 you're not store the data... only the operations.
the main principle of OOP is Reusable Code, so create a class for a user you should be able to reuse the code over and over again.
Every object is unique to an entity, in most cases, example of what i mean below:
class identification
User user id
Curl Web Address
Logger Log File
With an objects you should pass in the identification with the constructor, so user
would be like so:
$Robert = new User(12);
$Peter = new User(15);
because your using the constructor each class can only be created once as its dedicated to its identifier.
In the user class you would be able to create methods like so:
public function addFriend(User $User)
{
//Insert into the database with $User->getID();
}
$Robert->addFriend($Peter);
in specific regards to your class I would certainly take on Philippe Gerber's ethod as this to me is the standard.
You also should take not that in Philippe Gerber's method he is never changing the variable to upper-case but mealy returning an upper-case version of that string, the reason for this is that if you changed the state of the string during runtime it may not work as expected later in the process.
You should only ever change variables data if they have been replicated in the database or on file, so not matter what your script does not gets messed up.
精彩评论