fork(1) download
  1. <?php
  2.  
  3. class Button
  4. {
  5. public $data = []; //public только для теста
  6.  
  7. public function build($method)
  8. {
  9. if(is_callable($method)) {
  10. $method($this); // Передаем в колбэк $this (object Button)
  11. }
  12.  
  13. if(is_array($method)) {
  14. $this->data['buttons'] = $method;
  15. }
  16. }
  17.  
  18. public function addButton(array $button = [])
  19. {
  20. $this->data['buttons'] = $button;
  21.  
  22. return $this;
  23. }
  24. }
  25.  
  26. $button = new Button;
  27.  
  28. $button->build(function ($button) {
  29. /** @var Button $button */
  30. $button->addButton(['a' => 'b']);
  31. });
  32.  
  33. print_r($button->data['buttons']);
  34.  
Success #stdin #stdout 0.03s 23520KB
stdin
Standard input is empty
stdout
Array
(
    [a] => b
)