PHP Passing Data to a Specific Class? (Data Encapsulation)
Posted
by
Adam184
on Stack Overflow
See other posts from Stack Overflow
or by Adam184
Published on 2012-10-21T22:32:23Z
Indexed on
2012/10/21
23:00 UTC
Read the original article
Hit count: 104
php
I've learned that OOP is all about data encapsulation, but what about passing data between classes that have nothing to do with each other (would the below example be worthy of using extends
)?
class Dog {
private $secretVar;
public function getSecretVar() {
$this->secretVar = 'psst... only for rainbow!';
return $this->secretVar;
}
}
class Rainbow {
public function __construct(Dog $Dog) {
print_r($Dog->getSecretVar());
}
}
$Dog = new Dog();
$Rainbow = new Rainbow($Dog);
// ... classes that don't need the $secretVar
How would you encapsulate $secretVar
for only classes Dog
and Rainbow
? As of now, anyone can call getSecretVar()
, and I'm having a hard time allowing that to happen as it seems to defeat the whole point of encapsulation.
© Stack Overflow or respective owner