PHP-də OOP-də Private metodlar

Təkcə property-lər deyil, metodlar da private ola bilər. Adətən köməkçi metodlar private edilir ki, təsadüfən class xaricindən çağırıla bilməsinlər.

Nümunəyə baxaq. Tutaq ki, aşağıdakı class-ımız var:

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

Gəlin bu class-da köməkçi bir metod yaradaq, parametr olaraq bir sətir qəbul etsin və onun birinci simvolunu böyük hərf etsin:

php<?php
  class User {
    private $name;
    
    public function __construct($name) {
      $this->name = $name;
    }
    
    public function show() {
      return $this->name;
    }
    
    private function cape($str) {
      return mb_strtoupper(mb_substr($str, 0, 1)) . mb_substr($str, 1);
    }
  }
?>

İndi isə bu köməkçi metodumuzdan digər bir metodun daxilində istifadə edək:

php<?php
  class User {
    private $name;
    
    public function __construct($name) {
      $this->name = $name;
    }
    
    public function show() {
      return $this->cape($this->name);
    }
    
    private function cape($str) {
      return mb_strtoupper(mb_substr($str, 0, 1)) . mb_substr($str, 1);
    }
  }
?>

Gəlin yoxlayaq. Class-ımızın obyektini yaradaq:

php<?php
  $user = new User('john');
?>

Köməkçi metoddan istifadə edən public metodu çağıraq:

php<?php
  echo $user->show();
?>
Задание 1

Aşağıdakı kodda köməkçi metodu private edin:

php<?php
  class Employee {
    public function __construct($name, $salary) {
      $this->name = $name;
      $this->salary = $salary;
    }
    
    public function getSalary() {
      return $this->addSign($this->salary);
    }
    
    public function addSign($num) {
      return $num . '
;
    }
  }
?>