Can someone explain why a function is being used within a class? [php]


<?php
class Fruit {
  // Properties
  public $name;
  public $color;

  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}
?>

The above code is on the website w3schools. I don’t really understand why a method is being used here to create an instance of the class Fruit .

Isn’t it easier to do the below code instead?


$Fruit1 = new Fruit;
$Fruit->name = "apple";
$Fruit->color = "red";

The method is making me confused.

Hi,
It is because PHP is a dynamic language.

A language has dynamic typing when it does not associate values strictly with a specific type, but it is designed to “decide” what the type of a value should be at runtime, based on how you are attempting to use it.

PHP allows us to use dynamic variables. Dynamic variable is variable variables. We can name a variable with the value stored in another variable. That is, one variable contains the name of another variable.

When using $this->name , you are accessing the property with the name name of the current object

When using $this->$name , $name is determined before accessing the property – which means you’ll access the property which name is contained in the $name local variable.

1 Like

Hello there,
thanks for the reply. Just to make sure I have understood this properly.

function getName($name){
    $this->name = $name;
}

So if I pass a value to $name that value will be then given to the name property in the class Fruit. Am I correct?

So does that mean I can use any other variable inside the getName method?
Like:

function getName($fruitname){
    $this->name = $fruitname;
}

Thank you for your help

Hello! Welcome to the community :grin:!

What @anon58011934 said is correct. However, you seem to be mixing terms here:

<?php
class Fruit {
  // Properties
  public $name;
  public $color;

  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}
?>

In this code, unless you didn’t paste everything, no instance of the class Fruit is being created. I needed to clarify that :stuck_out_tongue:.

The methods defined in the class are known as getters and setters. These are useful, mostly, when the properties of the same name are private or protected, otherwise you could access them directly (as you correctly stated in your second paragraph). The idea of them being private or protected, is to add some kind of validation or logic when assigning a value to them, like:

<?php
class X {
    private $p;

    public function __construct() {
        $this->p = "initial value";
    }

    public function set_p($p) {
        if ($p) {
            throw new Exception("You must provide a value to the property 'p'");
        }

        $this->p = $p;
    }

    public function get_p() {
        return $this->p;
    }
}

In this case, you would forbid any user of your class (a user of the class being other developer that instantiates your class) to pass null or an empty value to your p property, thus ensuring it always has a value.

So, there’s no difference between using a method or directly accessing a public property. If they are protected or private, you cannot directly access them.

Yes, that’s correct. However, the setter should be the one that assigns a value, whereas a getter should only return a value.

So does that mean I can use any other variable inside the getName method?

Yes, the parameter name on the function can be called however you want :slight_smile:.

2 Likes