Jump to content

Oop Php For Beginners - Step 14 - Mistake?


drzin

Recommended Posts

Hey Stefan,

 

Please take another look at Step 14.

 

Having built a constructor in 'class person' in your class_lib.php file,

and both a set_name and get_name method,

it seems to me totally redundant, even though it will work, to place the set_name or

get_name method in your echo call back to the class.

 

Rather than

echo "Stefan's full name :" . $stefan->get_name();

 

The proper syntax would be:

echo "Stefan's full name: $stefan->name;

 

Would it not?

Link to comment
Share on other sites

See step 12. ;)

 

Talking about accessing properties directly:

"Though doable, it is considered bad practice to do it because it can lead to trouble down the road. You should use getter methods instead - more on that later."

 

Using setter/getter methods is considered better practice than accessing properties directly, since you want to ensure that any changes/access to those properties are handled within the object itself, not through an outside source.

Link to comment
Share on other sites

See step 12. ;)

 

Talking about accessing properties directly:

"Though doable, it is considered bad practice to do it because it can lead to trouble down the road. You should use getter methods instead - more on that later."

 

Using setter/getter methods is considered better practice than accessing properties directly, since you want to ensure that any changes/access to those properties are handled within the object itself, not through an outside source.

Link to comment
Share on other sites

So let me get this straight. The class itself is not an object, simply the spec sheet, setup manual and paradigm used only as a guide from which to correctly build one's own object. No operational object exists until it is assembled at its destination location. Therefore if one doesn't use the get method when calling the value with the 'echo' keyword, he is not utilizing his own personal object. Rather he is calling the paradigm model.

Link to comment
Share on other sites

Some yes, but mostly no.

 

Yes: A class itself is not an object. It's the structure for an object. For it to become an object, it needs to be instantiated with the "new" function:

 

$stefan = new person();

 

No: Basically everything else. :) Whether you are accessing an object's properties directly (example: "$stefan->name;"), or using a getter method (example: "$stefan->get_name()"), either way, you are accessing an object that has been created.

 

It is technically possible to access properties/methods without instantiating them in an object using the "static" keyword (http://php.net/manual/en/language.oop5.static.php), but that isn't really within the scope of your comment above.

 

---

 

Basically, the point is this. Say you create an object, and it has a property that holds a value. The object itself should be in control of the value of all of its properties, and it should have the option of rejecting outside changes if necessary. You shouldn't make it possible for outside code to change the value of a property without the object knowing about it. This is especially important if the object is expecting a property to hold a specific value -- if an outside source changes it, it may lead to bugs in your code due to incorrect expectations on the value of the property.

 

By using setters/getters, you are keeping the control within the object itself. For example, say in a setter method, you could set up restrictions so that certain values could not be passed in. For example, take the Person class that was created in the example on killerphp.com. I think we can safely assume that if someone is a person, they have a name. So, in the setter method for the object, you could include code that would reject any attempt to clear the person's name:

 

function set_name($new_name) {
   if ($new_name != '') {
       $this->name = $new_name;
   }
}

This way, the object itself has control over its properties, and it's properties can't be unexpectedly changed, or changed in a way that wouldn't make sense. Hopefully that makes sense?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...