The visibility of a property (public, private and protected) are part of the Concept of Withholding Information, which is important to achieve greater data consistency.
For example, the code below:
class Db_Table {
public $dbAdapter;
public function __constructor( Db_Adapter $dbAdapter ) {
$this -> dbAdapter = $dbAdapter;
}
}
There’s nothing to stop Cletus from taking that code to do something like:
$this -> dbAdapter = 'Oi, eu sou o Goku!';
And send the code to space by defining an anime ending catchphrase on what should be an object that implements an interface to the database or extends that superclass.
This problem is solved by changing the visibility of the property and creating a Setter:
class Db_Table {
private $dbAdapter;
public function __constructor( Db_Adapter $dbAdapter ) {
$this -> setAdapter( $adapter );
}
public function setAdapter( Db_Adapter $dbAdapter ) {
$this -> dbAdapter = $dbAdapter;
return $this;
}
}
And the code is now foolproof because the property Db_table::$dbAdapter invariably will be an instance of Db_adapter.
Also set the visibility of a property with private without a Setter defined makes it read-only in the context of the object.
However, it is possible yes manipulate the display property value private and protected through Reflection:
$obj = new Db_Table( new Db_Adapter );
try {
$reflector = new ReflectionProperty( 'Db_Table', 'dbAdapter' );
$reflector -> setAccessible( TRUE );
$reflector -> setValue( $obj, 'Oi, eu sou Goku!' );
var_dump( $reflector, $obj );
} catch( ReflectionException $e ) {
echo $e -> getMessage();
}
Although Reflection does not serve for this. u.u'
Encapsulation is already a totally different animal. It involves the principle of code reuse (DRY - Don’t Repeat Yourself) that does not exist only in Object Orientation.
Procedurally, the simple fact of creating a function to store a repetitive piece of code is already a form of encapsulation.
The difference is that with Object Orientation we have inheritance, composition, polymorphism and all these strange words that raise the potential of encapsulation to the maximum of its potential.
Finally, the Validation the one you referred to, already covered by the examples, is only possible through a Setter because you can’t have polymorphism or even conditionality on public property. They accept whatever you pass to them.
Original Author: Henrique Barcelos
Thank you, I was able to understand the concept, but as such "you can’t have polymorphism or even conditionality on public property", I didn’t understand this part.
– Thiago
Public property works exactly like a tax. The left side gets the right side the way it was defined. Through a method you can specify that a certain property can only be an object that extends a certain class or implements a specific interface (polymorphism) or you can use some conditionals within the method so that the parameters can be of several different types.
– Bruno Augusto
If my answer or that of another user has solved your problem, remember to mark it as solved. Your doubt today may be someone else’s tomorrow. ;)
– Bruno Augusto
Autor Original: Henrique Barcelos
. Wouldn’t it be nice to link or find the original text? I liked it :)– Math
Yes, but it seems that since he went to Spain the blog died. He has a nice way of explaining complicated things. I think he’s got an account here, but no movement.
– Bruno Augusto