Last active
December 15, 2015 11:49
-
-
Save aheadley/5255837 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// PRIVATE | |
class A { | |
private $_a = 'foo'; | |
public function get_a() { | |
return $this->_a; | |
} | |
} | |
class B extends A { | |
public function set_a($x) { | |
$this->_a = $x; | |
} | |
public function set_other_a($a, $x) { | |
$a->_a = $x; | |
} | |
} | |
$a = new A(); | |
$b = new B(); | |
// doesn't work because A::_a is private and thus invisible to B objects | |
$b->set_a('bar'); | |
// also doesn't work for the same reason | |
$b->set_other_a($a, 'bar'); | |
// this is all fine and expected | |
// PROTECTED | |
class C { | |
protected $_a = 'foo'; | |
public function get_a() { | |
return $this->_a; | |
} | |
} | |
class D extends C { | |
public function set_a($x) { | |
$this->_a = $x; | |
} | |
public function set_other_a($a, $x) { | |
$a->_a = $x; | |
} | |
} | |
$c = new C(); | |
$d = new D(); | |
// works, is fine | |
$d->set_a('bar'); | |
// works, but shouldn't because it lets any object that extends your class | |
// fuck with your objects | |
$d->set_other_a($c, 'bar'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment