geno11x11 Posted February 5, 2013 Report Share Posted February 5, 2013 (edited) I'm trying to understand OOP and class construction. I have found many examples of building classes, but I am not clear on the purpose of one step: Here's some code: <?php class person { var $name; function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } ?> My question involves the statement: $this->name = $new_name. I understand the usage of $this and that the variable name is being set to the value of $new_name. Since $new_name has no value, it appears the statement may be used to create an element of the class. So, in the process of building a class, what does this statement do, and why is it required? Edited February 5, 2013 by Geno Quote Link to comment Share on other sites More sharing options...
falkencreative Posted February 5, 2013 Report Share Posted February 5, 2013 In and of itself, that doesn't do anything. However, if you were to create an object: $person = new person(); $person->set_name('Bob'); echo $person->get_name(); you can use that method to set the name. $new_name has a value of whatever is passed into the set_name method when it is called (for example, "Bob"). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.