[JAVA] [PHP] For those who are worried about weight ~ Private special case ~

How was the result of the medical examination, everyone?

Well, did the doctor tell you that you were "overweight" again? It's hard because my wife gets angry and my boss tells me to lose weight.

If so, keep your weight secret with the private property!

class Person
{
  private $name;

  //Top secret
  private $weight;

  public function __construct($name, $weight)
  {
    $this->name = $name;
    $this->weight = $weight;
  }
}

Yes, you can hide your weight with this. I'm happy.

Try adding a method

But you want to know the weight of that colleague who seems to be fatter than you. [^ 1]

[^ 1]: What is fun to know? You want to immerse yourself in the superiority complex of "I'm thinner than him."

Yes, I wrote this method for you.

class Person
{
  private $name;
  private $weight;

  public function __construct($name, $weight)
  {
    $this->name = $name;
    $this->weight = $weight;
  }

  /**
   *Compare if they have the same weight
   */
  public function isWeightEqual($other)
  {
    return $this->weight === $other->weight;
  }
}

Oh, but $ other-> weight is a private property, so isn't it? Those who thought.

Sorry. This works fine.

$a = new Person("Yamada Taro", 60.5);
$b = new Person("Jiro Tanaka", 60.5);
var_dump($a->isWeightEqual($b)); //--> bool(true)

If you do your best, you will be able to make ʻisWeightGreaterThan () and ʻisWeightLessThan ().

** It's no use trying to hide your weight! ** **

Think seriously

Actually, it is properly written in Access rights from other objects in the PHP manual. ..

Objects of the same type can access each other's private and protected members, even if they are not the same instance. This is because the implementation details of the object are known inside the object.

It says something difficult, but in short *** you can access private and protected members even if the instances are different if they are the same type ***.

Why are you doing such a troublesome thing?

Actually, this specification is the same in Java, but [^ 2], [Java] Why can I access the private fields of other instances if they are in the same class? I found a very good comment on the article.

[^ 2]: It seems to be the same in C ++ and C #. See Accessing private member variables.

@shirakamus If private is a specification that can only be accessed from this, what happens to the implementation of the equals method ...

In Java, object comparisons are done with the ʻequals () method. Every object inherits ʻObject.equals () as follows:

public boolean equals(Object o) {
    return this == o;
}

This is usually sufficient, but you can also override the ʻequals ()` method. Certainly, it looks like this. [^ 3]

[^ 3]: Actually, it seems that you have to override hashCode () as well, but I'll take measures once. For more information, see Follow the general contract when overriding equals.

public class Person
{
  private String name;
  private double weight;

  Person(String name, double weight)
  {
    this.name   = name;
    this.weight = weight;
  }

  @Override
  public boolean equals(Object other)
  {
    if (this == other) {
      return true;
    }

    if (other instanceof Person) {
      Person person = (Person) other;

      //You are accessing a private field! !! !!
      return person.name == this.name
        && person.weight == this.weight;
    }

    return false;
  }
}

The important part is the person.weight == this.weight part. This person.weight is the private field of the instance passed as an argument, but it is of the same Person type and can be accessed.

If this isn't possible, you'll have to go through the getter, like person.getWeight ().

Summary

There is no ʻequals () `like Java in PHP, but when we adopted object-oriented as an idea, we probably made it behave like Java and other object-oriented languages (probably). ).

Well, when asked if I should use it positively, it seems that it is not well known, so I think it is better to stop it.

Also, in the first place, if you lose weight, you shouldn't have to worry about such troublesome things.

** So you should lose weight. ** **

(2018/1/13) I added a subtitle.

Recommended Posts

[PHP] For those who are worried about weight ~ Private special case ~
Java Programmer Gold SE 8 Qualification Summary (for those who are familiar with Java)
For those of you who are thinking that Spring can't read JS?