DEV Community

Cover image for Using value-objects in Laravel Models

Using value-objects in Laravel Models

Benjamin Delespierre on July 26, 2021

(image credits the Joomla Community) Value Objects A value object is a small object that represents a simple entity whose equality is ...
Collapse
 
systemovich profile image
Geoffrey Van Wyk

If $value is private, then this comparison:


    public function equals(self $status): bool
    {
        return $this->value == $status->value;
    }
Enter fullscreen mode Exit fullscreen mode

should not be possible?

Collapse
 
bdelespierre profile image
Benjamin Delespierre

In PHP encapsulation is done at class level, not object level, so you can "visit" other instances' private properties value.

Collapse
 
systemovich profile image
Geoffrey Van Wyk

I stand corrected.

Objects of the same type will have access to each others private and protected members even though they are not the same instances.

Collapse
 
systemovich profile image
Geoffrey Van Wyk

PHP needs a __equals magic method :)

Collapse
 
bdelespierre profile image
Benjamin Delespierre

That would be fantastic. How about you draft an RFC for that?

Collapse
 
systemovich profile image
Geoffrey Van Wyk • Edited

Someone proposed it for PHP 7.3, but it was declined: PHP RFC: User-defined object comparison

It is difficult to find the reasons for why it was declined: github.com/php/php-src/pull/3339#i...

As someone mentioned on a PHP podcast, the PHP RFC voting system needs a lot of improvement.

Thread Thread
 
bdelespierre profile image
Benjamin Delespierre

I understand. While things could certainly be better, I think we need to be careful: changing the RFC system could jeopardize the backward-compatibility, which is IMHO the best aspect of the platform.

Collapse
 
mikejtz profile image
Michel JAUTZY

Great article, great usecase !

Collapse
 
bdelespierre profile image
Benjamin Delespierre

Thanks 🙏

Collapse
 
shifrin profile image
Mohammed Shifreen

Is it really necessary to make constructor private for final class?

Collapse
 
jovialcore profile image
Chidiebere Chukwudi

NIce one