Code Monkey home page Code Monkey logo

Comments (4)

frederikbosch avatar frederikbosch commented on August 30, 2024

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

from mail.

Zolli avatar Zolli commented on August 30, 2024

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

from mail.

frederikbosch avatar frederikbosch commented on August 30, 2024

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('[email protected]');
$address2 = new EmailAddress('[email protected]');
$address3 = new EmailAddress('[email protected]');

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

from mail.

Zolli avatar Zolli commented on August 30, 2024

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

from mail.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.