Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible error in EmailAddress #6

Closed
Zolli opened this issue Jun 14, 2017 · 4 comments
Closed

Possible error in EmailAddress #6

Zolli opened this issue Jun 14, 2017 · 4 comments

Comments

@Zolli
Copy link

Zolli commented Jun 14, 2017

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.

@frederikbosch
Copy link
Contributor

frederikbosch commented Jun 14, 2017

@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;
  }
}

@Zolli
Copy link
Author

Zolli commented Jun 14, 2017

Oh, sorry about that. I dont use this feature very often.

@Zolli Zolli closed this as completed Jun 14, 2017
@frederikbosch
Copy link
Contributor

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 (!)

@Zolli
Copy link
Author

Zolli commented Jun 14, 2017

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants