PHP'de OOP'de Nesne Oluşturulduktan Hemen Sonra Metot Çağrısı

Bize, bir dizi sayıyı saklayan ve getSum metodu ile bu sayıların toplamını hesaplayabilen bir Arr sınıfı verildiğini varsayalım. Sayıların kendisi bir dizi olarak nesnenin yapıcısına (constructor) gelir ve ayrıca add metodu ile tek tek eklenebilir:

php<?php
  class Arr
  {
    private $numbers = [];
    
    public function __construct($numbers)
    {
      $this->numbers = $numbers;
    }
    
    public function add($number)
    {
      $this->numbers[] = $number;
    }
    
    public function getSum()
    {
      return array_sum($this->numbers);
    }
  }
?>

İşte Arr sınıfının kullanımına bir örnek:

php<?php
  $arr = new Arr([1, 2, 3]);
  
  $arr->add(4);
  $arr->add(5);
  
  echo $arr->getSum(); // 15 yazdırır
?>

Nesne oluşturma anında tüm gerekli sayıları ileteceğimiz ve ardından hemen toplamlarını bulmak isteyeceğimiz durumlar olabilir:

php<?php
  $arr = new Arr([1, 2, 3]);
  echo $arr->getSum(); // 6 yazdırır
?>

Nesneyle başka herhangi bir işlem yapmayı planlamıyorsak, yukarıdaki kodu daha kısa bir şekilde yeniden yazabiliriz: nesneyi oluşturup hemen getSum metodunu çağırabiliriz:

php<?php
  echo (new Arr([1, 2, 3]))->getSum(); // 6 yazdırır
?>
Задание 1
php<?php
  class StringProcessor
  {
    private $str = '';
    
    public function __construct($text)
    {
      $this->str = $text;
    }
    
    public function append($text)
    {
      $this->str .= $text;
      return $this;
    }
    
    public function getValue()
    {
      return $this->str;
    }
    
    public function getLength()
    {
      return strlen($this->str);
    }
  }
?>

Bu sınıfla şu şekilde çalışıldığını varsayalım:

php<?php
  $str = new Str('aaa');
  $str->append('bbb');
  $str->append('ccc');
  
  echo $str->getLength();
?>

Bu kodu, sınıfı yerinde (doğrudan) çağırarak yeniden yazın.