Untitled

 avatar
unknown
php
5 months ago
1.2 kB
4
Indexable
<?php

class Employee {
    public $id;
    public $name;
    public $role;

    public function __construct($id, $name, $role) {
        $this->id = $id;
        $this->name = $name;
        $this->role = $role;
    }

    public function calculatePay() {
        if ($this->role === 'developer') {
            return 5000;
        } elseif ($this->role === 'manager') {
            return 8000;
        } else {
            return 3000;
        }
    }

    public function save() {
        $filename = $this->id . '.txt';
        $data = 'ID: ' . $this->id . ', Name: ' . $this->name . ', Role: ' . $this->role;
        file_put_contents($filename, $data);
    }
}

class EmployeeReport {
    public $employee;

    public function __construct($employee) {
        $this->employee = $employee;
    }

    public function report() {
        echo 'Report for ' . $this->employee->name . ', Role: ' . $this->employee->role . ', Pay: ' . $this->employee->calculatePay() . PHP_EOL;
    }
}

// Usage example
$e1 = new Employee(1, 'Alice', 'developer');
$e2 = new Employee(2, 'Bob', 'manager');
$e1->save();
$e2->save();

$report = new EmployeeReport($e1);
$report->report();

?>
Editor is loading...
Leave a Comment