Possible error in EmailAddress
Zolli opened this issue · 4 comments
While I looking at the code I ran into the EmailAddress::equals() method. The method expect an EmailAddress as first parameter and use this to read the property called address ($address->address), but this property has private visibility. Probably submit a PR for that.
@Zolli In PHP instances of the same class can access their private properties. This is a feature of PHP, not a bug.
So.
<?php
class MyObject {
private $property = 'x';
public function equals(MyObject $object) {
// because $object and $this are of the same class (MyObject)
// the property can be accessed even though it is private
// this is because php only prohibits other *classes* - and thus not instances - to access private properties
return $this->property === $object->property;
}
}
Oh, sorry about that. I dont use this feature very often.
No problem. You might also have a look at the tests. This functionality is tested here.
Otherwise, I might also recommend reading upon value objects, or specific for PHP. In this case EmailAddresss
is a value object. One of the characteristics of a value object is that their equality is based on the value, not on identity.
So.
$address1 = new EmailAddress('test@test.com');
$address2 = new EmailAddress('test@test.com');
$address3 = new EmailAddress('xxx@test.com');
$address1->equals($address2); // true
$address1->equals($address3); // false
This is different from entities and models. For models and entities the equality is based on the id of the object.
$user1 = new User(['id' => 1]);
$user2 = new User(['id' => 2]);
$user3 = new User(['id' => 3]);
$user1->equals($user2); // true
$user1->equals($user3); // false
Hope it helps understanding the concept! It is really valuable (!)
@frederikbosch, I understand the concept, only those private access from the same type behavior not. Thanks for write this down. This could be very useful.