Php OOP – Messages 101


This is just a sample I created for learning purposes base on jeffrey way video  sample

Its really much the same with the video but I just wanted to write it down by me self so that I will understand the flow and how it works

Php OOP Messages 101

class Person {

   protected $name;

   public function __construct($name) {

      $this->name = $name;

   }

}

class Business {

  protected $staff;

  public function __construct(Staff $staff) {

    $this->staff = $staff;

  }

  public function hire(Person $person) {

    $this->staff->add($person);

  }

  public function getStaffMembers() {

   return $this->staff->members();

  }

}
class Staff {

        protected $members = [];

        public function __construct($member = []) {

                $this->members[] = $member;

        }

        public function add(Person $person) {

                $this->members[] = $person;

        }

        public function members() {

                return $this->members;

        }

}

$staff = new Staff(['John Owner']);
$jollibee = new Business($staff);

$yolan = new Person('Yolan');
$apol = new Person('Apol');
$chololoy = new Person('Chololoy');

$jollibee ->hire($yolan);
$jollibee ->hire($apol);
$jollibee ->hire($chololoy);

var_dump($jollibee->getStaffMembers());

Published by

Emisyl

Is a web developer and a Php enthusiasts base in the Philippines

Other sites
https://planetcodes.blogspot.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Emisyl