1

Get and set methods in PHP

As you know, in php there are classes that are pretty useful at the moment to write code. Their usefulness lyes in the fact that with classes we write less and we can focus more creating. So, there is a problem even although the classes are pretty good in php and the problem is that we cannot declarate variables at the moment to create a class. For example the following block of code is wrong:

class animal($name, $lastName){
	$this -> name = $name;
	$this -> lastName = $lastName;
}

That’s the reason why the people use constructors that are functions where we can construct our object. Anyway, the point of this article is to give an example of the getters and setters. Which are key elements that allow us to declarate (in this case) the name of the animal, maybe the type of animal, etc.

So, check it out the following example carefully:

<?php
        class Man{
                private $name;
                private $lastName;
                private $age;

                function setName($name){
                        $this -> name = $name;
                }
                function setLastName($lastName){
                        $this -> lastName = $lastName;
                }
                function setAge($age){
                        $this -> age = $age;
                }
                function describeYourself(){
                        print "Hello, my name is $this->name $this->lastName and I am $this->age years old. \n";
                }
        }

        $javier = new Man();
        $javier -> setName("Javier");
        $javier -> setLastName("Marin");
        $javier -> setAge(59);
        $javier -> describeYourself();
?>

in this case we are using only setters but in the following block of code I am going to add the getters:

<?php
        class Man{
                private $name;
                private $lastName;
                private $age;

                function setName($name){
                        $this -> name = $name;
                }
                function setLastName($lastName){
                        $this -> lastName = $lastName;
                }
                function setAge($age){
                        $this -> age = $age;
                }
                function describeYourself(){
                        print "Hello, my name is $this->name $this->lastName and I am $this->age years old. \n";
                }
                function getName(){
                        return $this -> name;
                }
        }

        $javier = new Man();
        $javier -> setName("Javier");
        $javier -> setLastName("Marin");
        $javier -> setAge(59);
        $javier -> describeYourself();
        print "Name: ".$javier -> getName()."\n";
?>

The result will look something like:

Hello, mynameis Javier Marin and I am 59 years old.
Name: Javier

If you nortice, I only add the getName function. This was intentionally because your challenge is to completer the other getters such as:

  • getLastName()
  • getAge()

Add as much attributes as you want as long as the attributes makes sense.

Escribe tu comentario
+ 2