<?php
mb_internal_encoding('utf-8');
class Employee
{
    public $name;
    public $rate;
    public $hours = array();
    public function __construct($name, $rate)
    {
        $this->name = $name;
        $this->rate = $rate;
    }
    public function getTotalHoursWorked()
    {
        return array_sum($this->hours);
    }
    public function getNormalHours()
    {
        $normal = 0;
        foreach ($this->hours as $week) {
            if ($week <= 40) {
                $normal = $week + $normal;
            } else {
                $normal = 40 + $normal;
            }
        }
        return $normal;
    }
    public function getOvertimeHours()
    {
        $overtime = 0;
        foreach ($this->hours as $week) {
            if ($week > 40) {
                $overtime = ($week - 40) + $overtime;
            } else {
                $overtime = $overtime + 0;
            }
        }
        return $overtime;
    }
    public function getSalary()
    {
        $hours    = $this->getNormalHours();
        $overtime = $this->getOvertimeHours();
        $salary   = $hours * $this->rate + 2 * $this->rate * $overtime;
        return $salary;
    }
    public function getShortName()
    {
        $firstletter = mb_substr($this->name, 0, 1) . ".";
        $reg         = '/[а-яА-Яё]+\s/u';
        $shortname   = preg_replace($reg, "", $this->name) . " " . $firstletter;
        return $shortname;
    }
}

$ivan = new Employee('Иван Иванов', 10);


$ivan->hours = array(
    40,
    40,
    40,
    40
);

$peter = new Employee('Пётр Петров', 8); //с другими именами всё работает


$peter->hours = array(
    40,
    10,
    40,
    50
);

$employees = array(
    $ivan,
    $peter
);

$col1 = 30;
$col2 = 8;
$col3 = 8;
$col4 = 8;


function padRight($string, $length)
{
    while (mb_strlen($string) < $length) {
        $string = $string . " ";
        
    }
    return $string;
}
function padLeft($string, $length)
{
    while (mb_strlen($string) < $length) {
        $string = " " . $string;
    }
    return $string;
}

//заголовок таблицы

echo padRight("Сотрудник", $col1) . padLeft("Ставка", $col2) . padLeft("Часы", $col3) . padLeft("Овертайм", 10) . padLeft("З/П", $col4) . "\n\n";

//сама таблица
$hours    = 0;
$salary   = 0;
$overtime = 0;

foreach ($employees as $employee) {
    echo padRight($employee->getShortName(), $col1) . padLeft($employee->rate, $col2) . padLeft($employee->getTotalHoursWorked(), $col3) . padLeft($employee->getOverTimeHours(), 10) . padLeft($employee->getSalary(), $col4) . "\n";
    $hours    = $hours + $employee->getTotalHoursWorked();
    $salary   = $salary + $employee->getSalary();
    $overtime = $overtime + $employee->getOverTimeHours();
    
}
echo padRight("Всего", 30) . padLeft("", $col2) . padLeft($hours, $col2) . padLeft($overtime, 10) . padLeft($salary, $col2);