View on GitHub

RefactoringPHP

A PHP version of the refactors from Refactoring: Improving the Design of Existing Code (Second Edition) by Martin Fowler

Encapsulate Collection (170)

Old Code

<?php
class Person
{
    protected $courses;
    public function getCourses()
    {
        return $this->courses;
    }
    
    public function setCourses($aList)
    {
        $this->courses = $aList;
    }
}

New Code

<?php
class Organization
{
    protected $courses;
    public function getCourses()
    {
        return $this->courses;
    }
    
    public function addCourse($aCourse) {
        // add course here
    }
    
    public function removeCourse($aCourse) {
        // remove course here
    }
    
}