Created
September 26, 2017 02:15
-
-
Save buiquangduc/9b0f5ca38fffbb8b0bfb0ec6802ed0a4 to your computer and use it in GitHub Desktop.
Don't expose member data in public
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Example of exposing member data in public | |
// Coding Horror | |
class Class | |
{ | |
public $student; | |
public $teacher; | |
} | |
// Example of good encapsulation (don't expose member data in public) | |
class Class | |
{ | |
protected $student; | |
protected $teacher; | |
public function setStudent($student) { | |
$this->student = $student; | |
} | |
public function setTeacher($teacher) { | |
$this->teacher = $teacher; | |
} | |
public function getStudent() { | |
return $this->student; | |
} | |
public function getTeacher() { | |
return $this->teacher; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment