Are setter methods needed when constructor sets properties?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    Are setter methods needed when constructor sets properties?

    Hi,
    I have just started learning OOP
    and have a couple of questions.

    Is it correct to say that if I write a constructor method for my class
    then I will not need to use he set method to add values to properties ?

    i.e. with this:

    Code:
    <?php
    class person {
      var $name;
      var $sp_number; 
          public $height;
          protected $social_insurance;
          
    function __construct($persons_name) {
        $this->name = $persons_name;
      }
    
    function get_name() {
        return $this->name;
      }
    		
    } // end class
    ?>

    this will not be needed:

    Code:
     function set_name($new_name) {  
       $this->name = $new_name;            
      }
    because in my html I will use this:

    Code:
    <?php
    $stefan = new person("Stefan Mischook"); 			
    
    echo "Stefan's full name: " . $stefan->get_name()."<br>";
    ?>
    True - or have I got things mixed up ?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by jeddiki
    Hi,
    I have just started learning OOP
    and have a couple of questions.

    Is it correct to say that if I write a constructor method for my class
    then I will not need to use he set method to add values to properties ?

    i.e. with this:

    Code:
    <?php
    class person {
      var $name;
      var $sp_number; 
          public $height;
          protected $social_insurance;
          
    function __construct($persons_name) {
        $this->name = $persons_name;
      }
    
    function get_name() {
        return $this->name;
      }
    		
    } // end class
    ?>

    this will not be needed:

    Code:
     function set_name($new_name) {  
       $this->name = $new_name;            
      }
    because in my html I will use this:

    Code:
    <?php
    $stefan = new person("Stefan Mischook"); 			
    
    echo "Stefan's full name: " . $stefan->get_name()."<br>";
    ?>
    True - or have I got things mixed up ?
    You may later want to change the person's name. Maybe he/she gets married and take's the other person's name? Should always have getter and setter methods, I believe.

    Also, could you please read your Personal Messages.

    Thanks,
    Markus.

    Comment

    • jeddiki
      Contributor
      • Jan 2009
      • 290

      #3
      Following on from that question is another
      basic newbie one:

      Again using this class:

      class_lib.php
      Code:
      class person {
        var $name;
        var $sp_number; 
        public $height;
        protected $social_insurance;
            
       
        function __construct($persons_name) {
          $this->name = $persons_name;
        }
      	
      	
        function set_name($new_name) {  
         $this->name = $new_name;            
        }
      
        function get_name() {
          return $this->name;
        }
      	
      } // end class
      To apply values to the name property I use this:

      <?php
      $stefan = new person("Stefan Mischook");
      ?>
      How do I add a value to the $sp_number ?

      I guess that I can not use the constructor again
      so do I add this to class_lib.php ?

      Code:
        function set_sp_number($new_sp_number) {  
         $this->sp_number = $new_sp_number;            
        }
      and then use it like this ?

      Code:
      $stefan->set_sp_number(8872);
      Any explaination would be greatly appreciated :)

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        You can pass multiple arguments to a method.

        Code:
        class Example
        {
        
            public $firstName;
            public $lastName;
        
            // Constructor
            function Example($first, $last)
            {
                $this->firstName = $first;
                $this->lastName = $last;
            }
        
        }
        
        $ex = new Example("John", "Dorian");

        Comment

        • jeddiki
          Contributor
          • Jan 2009
          • 290

          #5
          Thats fine but what if I need to add the $lastName at a
          later date ( eg when they fill out a profile form).

          How do I add the $lastName then - I guess a should not
          use :

          Code:
          $ex = new Example( , "Dorian");
          or could I ?

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            You can set default values for arguments on methods. So you can provide either a first name and a last name, or just a first name.

            Code:
            function Example($first, $last = "")
            {
                ....
            }
            And you could set the last name later, with a setter method.

            Code:
            $ex->set_lastName("Dorian");
            You don't call your constructor after you have initialised the class, i.e after you have done $ex = new Example().

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by jeddiki
              How do I add the $lastName then - I guess a should not
              use :
              Code:
              $ex = new Example( , "Dorian");
              correct, PHP will throw an error in this case.

              to add the name either use the setter method or set the property directly, if it is defined public.
              Code:
              $ex->lastName = "Dorian";
              note: the var keyword is not encouraged in PHP 5, it is simply kept for compatibility reasons towards PHP 4

              Comment

              • jeddiki
                Contributor
                • Jan 2009
                • 290

                #8
                Do you mean the var that I put in my class_lib.php ?

                Code:
                <?php
                
                class person {
                  var $name;
                	var $sp_number; 
                      public $height;
                      protected $social_insurance;

                If so how should I be writing it in that class definition ?

                BTW I am reading through the PHP 5 Power Programming by Andi Gutmans
                so hopefully I'll get better at this ;)

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  Originally posted by jeddiki
                  Do you mean the var that I put in my class_lib.php ?

                  If so how should I be writing it in that class definition ?
                  a) yes

                  b) that depends on the usage, possible values
                  • public (external read/write access)
                  • protected (internal access across inheritance)
                  • private (access only inside the defining class)

                  static, const and read-only are covered later
                  Last edited by Dormilich; Jan 25 '09, 05:37 PM. Reason: added const keyword

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    You should declare variables as protected, private or public (usually).

                    So don't use the 'var' keyword, but any of the above. It's not going to give you any trouble, though.


                    -_- nvm.

                    Comment

                    Working...