Get and Set methods in php

Discussion in 'PHP' started by arunch2007, Mar 23, 2010.

  1. #1
    hello
    i m bit confused in get and set method what are these how to use them and how important they are.

    thanx in advance
    regards
    Arun
     
    arunch2007, Mar 23, 2010 IP
  2. waterbomm

    waterbomm Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I found it from Google
     
    waterbomm, Mar 23, 2010 IP
  3. angelomaniac

    angelomaniac Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    probably you means get & set in PHP Class, right?

    The getter and setter are the public functions which is allowed to modify / get the value of private variables in Classes. So if you have severals private variables in a Class, you should create a getter function to get the value & setter function to modify the value.
     
    angelomaniac, Mar 23, 2010 IP
  4. arunch2007

    arunch2007 Active Member

    Messages:
    214
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #4

    very confusing
     
    arunch2007, Mar 31, 2010 IP
  5. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #5
    Let me try and throw in my 2 cents. With classes in PHP 5, there are three visibility keywords: public, private and protected. Public properties can be access from any context, provided the class object has been initialized, private properties are only accessible in the class they were declared in, while protected properties are similar to private variables, although they can also be accessed from child classes.

    Firstly, getters and setters are used either on private or protected properties. As a programmer, you want to eliminate as many possible sources of errors as you can. For instance, lets look at the following example:

    class Foo {
    
        public $Amount = 10;
    
        public function LoopAmount() {
            for( $x = 1; $x <= $this->Amount; $x++ ) {
                print $x . "\n";
            }
        }
    }
    PHP:
    As you should be able to see, the LoopAmount() method loops from 1-10, with the $Amount property public and thus able to be edited. So, if we wanted to loop through 20 times we could use:

    $Foo = new Foo();
    $Foo->Amount = 20;
    $Foo->LoopAmount();
    PHP:
    Which would work fine. However, let's assume an amateur programmer is using our class. They may think they have to do it as so:
    $Foo = new Foo();
    $Foo->Amount = "twenty";
    $Foo->LoopAmount();
    PHP:
    Clearly the above script wouldn't loop through 20 times, as "twenty" is a string, not an integer. Therein lies a small issue with PHP. Apart from arrays, you can't really force a property type. Which is why, in many situations, we're forced to use setters and getters to ensure that proper property types are adhered to. This eliminates the possibilty of user error. For instance:

    class Foo {
    
        private $Amount = 10;
    
        public function SetAmount( $Am ) {
            if( ! is_int( $Am ) ) {
                die( "Amount variable must be a valid integer." );
            }
            $this->Amount = $Am;
        }
    
        public function LoopAmount() {
            for( $x = 1; $x <= $this->Amount; $x++ ) {
                print $x . "\n";
            }
        }
    }
    PHP:
    In the above example, we have gone and made the $Amount property private, preventing users from directly editing it. Instead, they have to go through the setter function, which makes sure its a valid number prior to updating the property. The example I've just given you exits in the event of unwanted property types, which is a bit extreme. Setters often convert to the desired property types, or log to a debug file, etc.

    -Alex.
     
    Alex Roxon, Mar 31, 2010 IP