fork(1) download
  1. <?php
  2.  
  3. class Worker {
  4. // An array with workers
  5. protected $workers = array(
  6. 'engineer' => array(
  7. 'salary' => 100
  8. ),
  9.  
  10. 'marketer' => array(
  11. 'salary' => 200
  12. )
  13. );
  14.  
  15. // An array of worker params
  16. protected $current_worker;
  17.  
  18. /**
  19.   * Worker constructor.
  20.   */
  21. public function __construct( $job_title = 'marketer' ) {
  22. // Check if such position exists
  23. if ( array_key_exists( $job_title, $this->workers ) ) {
  24. // Assign job title
  25. $this->current_worker = $this->workers[$job_title];
  26. } else {
  27. throw new Exception( 'Passed job title does not exist!' );
  28. }
  29. }
  30.  
  31. /**
  32.   * Getter for worker salary
  33.   */
  34. public function get_worker_salary() {
  35. // Check if salary key exists
  36. if ( array_key_exists( 'salary', $this->current_worker ) ) {
  37. return $this->current_worker['salary'];
  38. } else {
  39. return false;
  40. }
  41. }
  42. }
  43.  
  44. $Vasya = new Worker( 'engineer' );
  45. echo $Vasya->get_worker_salary();
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
    100