dannyman1234 Posted June 22, 2009 Report Share Posted June 22, 2009 Hi, Im just starting out with OOP in php and I am doing the basic tutorial in: http://www.killerphp.com/tutorials/object-oriented-php. Nice tut btw!! I have a question: So I have this class: class person { var $name; public $height; protected $social_insurance; private $pinn_number; function __construct($persons_name) { $this->name = $persons_name; } function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } private function get_pinn_number(){ return $this->pinn_number; } } Then In my php page, I make the object: $stefan = new person("Stefan Mischook"); echo "Stefan's full name: " . $stefan->get_name() ; What I don't understand is, How is de property $name being declared if you make the object $stefan = new person("Stefan Mischook")? In other words: person is the class, but how does php know that "Stefan Mischook" is set for $name? why is "Stefan Mischook" not set for $height or $social_insurance, which are also variables inside the person class.? Am I making any sense here?:/ Quote Link to comment Share on other sites More sharing options...
BeeDev Posted June 22, 2009 Report Share Posted June 22, 2009 It's to do with the function called __construct($persons_name) This function always runs when you declare a new object. So if you look at your __construct() function it takes in a variable $persons_name and it assigns $this->name = $persons_name; So when you call : $stefan = new person("Stefan Mischook"); it automatically assigns the given name when it runs the __construct function. Hope that makes sense. Quote Link to comment Share on other sites More sharing options...
dannyman1234 Posted June 22, 2009 Author Report Share Posted June 22, 2009 Yes thanks, it makes sense. But what about if i want to access the property "height"? I tried to make a function: function get_height($new_height) { $this->height = $new_height; return $this->height; } then in index.php $stefan = new person("Stefan Mischook"); echo "Stefan's full name: " . $stefan->get_name() ; echo $stefan->get_height('1.67'); Is this the right way to do it? I get the value printed. I read in the tut about getters and setters: function set_name($new_name) { $this->name = $new_name; return $this->name; } function get_name() { return $this->name; } Why do we need this? function set_name already returns $this->name;. Quote Link to comment Share on other sites More sharing options...
BeeDev Posted June 23, 2009 Report Share Posted June 23, 2009 set_name and set_height functions should not be returning values. They should return true or false in case of success of setting or failure. However, on the other hand, get_ functions should always return a value. But really it's up to you to make the decision on whether a function should set or get a value, or do both. The point is to adapt to your specific need. Quote Link to comment Share on other sites More sharing options...
Guest WebElf Posted July 8, 2009 Report Share Posted July 8, 2009 To get your head around things you need to have a clear idea of where you are standing. Set functions store data in a class. Get functions retrieve stored data. eg. function get_height() { return $this->height; } function set_height($new_height) { $this->height = $new_height; } If you want to set the height when you create the person record you can redesign the constructer to accept name and height. Typically you put information a constructor that is neded to be set for all instances of a class. function __construct( $persons_name, $persons_height ) { $this->name = $persons_name; $this->height = $persons_height; } and call it via $stefan = new person( "Stefan Mischook", "1.67" ); then when you call get_height you will receive 1.67 back. Hope this helps. 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.