Accessing Properties Inside Classes in OOP in PHP

Inside class methods, a special variable $this will be available to us. It will point to the object of our class:

php<?php
  class User {
    public function show() {
      var_dump($this); // object
    }
  }
?>

This means that we can access the object's properties via $this. Let's try. Suppose our object has a property name. Let's output this property in our method:

php<?php
  class User {
    public $name;
    
    public function show() {
      return $this->name;
    }
  }
?>

Now let's create an object of our class:

php<?php
  $user = new User;
?>

Let's assign the property we need:

php<?php
  $user->name = 'john';
?>

Now let's call the method, thereby outputting the property value:

php<?php
  echo $user->show(); // will output 'john'
?>
Задание 1

Into the object of the Employee class write the properties name and salary.

Задание 2

Create a method that will output the employee's name to the screen.

Задание 3

Create a method that will output the employee's salary to the screen.