Jump to content

New OOP php question


dannyman1234

Recommended Posts

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!!:D

 

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?:/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 3 weeks later...
Guest WebElf

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.

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...