SenseException, German
@SenseException@phpc.social avatar

talks 10 years ago: "Use value objects instead of arrays."

PHP talks nowadays: "Use value objects instead of arrays."

Crell,
@Crell@phpc.social avatar

@SenseException I blame popular libraries and frameworks that are still array-based. (#Laravel and AWS-SDK at the top of that list. #Drupal, less than it used to be but still.) And people who cut their teeth on CodeIgnighter et al and never grew past that.

One of the many reasons I detest Laravel.

ramsey,
@ramsey@phpc.social avatar

@Crell @SenseException Arrays are just so easy to use in #PHP. Perhaps with property hooks and interface properties (combined with past changes to support property types), value objects will be easier.

Crell,
@Crell@phpc.social avatar

@ramsey @SenseException I don't think the new rfc does much to change that balance, sadly. If someone isn't tempted away from the dark side by property types, I don't see how interface properties will do it.

ramsey,
@ramsey@phpc.social avatar

@Crell @SenseException Maybe not, but I think it gives properties on objects a full story. I’m not sure at this point why we would need a struct-like data structure.

shadowhand,
@shadowhand@phpc.social avatar

@ramsey @Crell @SenseException the syntax for property hooks is very meh. Properties on interfaces is wonderful though. Very much looking forward to that.

ramsey,
@ramsey@phpc.social avatar

@shadowhand @Crell @SenseException The syntax doesn’t bother me. I’ll probably standardize on always specifying the argument, though, for the set hook (i.e., set(type $value) {} ).

heiglandreas,
@heiglandreas@phpc.social avatar

@ramsey Wait a second...

Interfaces in ValueObjects?

Which Memo did I not get?

/cc @Crell @SenseException

mwop,
@mwop@phpc.social avatar

@heiglandreas @ramsey @Crell @SenseException Part of the property hooks RFC is the ability to define properties with expected hooks on interfaces. (Won't work for generic properties; basically defines if a get, set or both hooks are required in the implementation.)

Crell,
@Crell@phpc.social avatar

@mwop @heiglandreas @ramsey @SenseException It absolutely will work with generic properties. A plain boring public property will satisfy an interface requirement for both get and set. A readonly public property will satisfy a get-only interface requirement.

ramsey,
@ramsey@phpc.social avatar

@Crell @mwop @heiglandreas @SenseException I think Andreas was reacting to my mention of interfaces in the same sentence as value objects. 🙂

heiglandreas,
@heiglandreas@phpc.social avatar

@ramsey @Crell @mwop @SenseException 🤣 I was indeed!

mwop,
@mwop@phpc.social avatar

@Crell ooooh! I missed that this introduced the general usage! Fun!

Crell,
@Crell@phpc.social avatar

@mwop

interface Named {
public string $name { get; }
}

class Bar implements Named {
public function __construct(public readonly string $name) {}
}

That should totally work. (To not need to go through the constructor we'll need asymmetric visibility, because of readonly's brokenness.)

mwop,
@mwop@phpc.social avatar

@Crell Right... But you can't have

interface Name {
public readonly string $value;
}

right? That's what I was getting at with the "only props with hooks in interfaces" remark.

Crell,
@Crell@phpc.social avatar

@mwop Ah. That isn't allowed, no. Specifically, the interface can say "this property must be readable" and/or "this property must be writeable." That's the minimum set of requirements you can place.

Those can be satisfied by plain properties or hooked properties, as your implementation prefers.

heiglandreas,
@heiglandreas@phpc.social avatar

@Crell @mwop I am not sure though why the interface declares an implementation detail.

The property hook should not be part of the API IMO. It's just an implementation detail of what to do with the passed information when setting or getting.

Or I completely missunderstood the whole concept....

sven,
@sven@phpc.social avatar

@mwop @heiglandreas @Crell in the example it just enforces that the $name property should be publicly accessible. That’s more than just an implementation detail if you ask me 😄

heiglandreas,
@heiglandreas@phpc.social avatar

@sven Yeah! That is definitely a tad more than an implementation detail! 😁

/cc @mwop @Crell

Crell,
@Crell@phpc.social avatar

@heiglandreas See my reply to @mwop just now.

deleugpn,
@deleugpn@fosstodon.org avatar

@ramsey @Crell @SenseException I doubt anything will ever be as easy as arrays

Crell,
@Crell@phpc.social avatar

@deleugpn @ramsey @SenseException Arrays are easy to write the first time, and hell to maintain the next day.

SenseException,
@SenseException@phpc.social avatar

@Crell @deleugpn @ramsey Not to mention that devs can stuff any data in any form or structure into an array (which is what you already called "hell to maintain"). It reminds me of RDBMS/SQL vs. Document DBs/NoSQL.

SenseException,
@SenseException@phpc.social avatar

"Use value objects instead of arrays" in 2024. #phptek
Onward, bringing this message to the next decade. #php
https://phpc.social/@ian/112328282730050276

dusoft,
@dusoft@fosstodon.org avatar

@SenseException Benchmarks? Faster, slower?

SenseException,
@SenseException@phpc.social avatar

@dusoft As far as I can remember objects became faster and faster over the last decade with newer php versions. I'm sure you'll find a history of such tests in Google.

The benefits of VOs are about structure, api, maintenance and/or archtecture.

acelaya,
@acelaya@mastodon.social avatar

@SenseException Does that mean we haven't learned anything? 😅

SenseException,
@SenseException@phpc.social avatar

@acelaya Kind of. This was just a prominent example.

adriano,
@adriano@lile.cl avatar

@SenseException they didn’t say “please”

SenseException, (edited )
@SenseException@phpc.social avatar

@adriano Kind of. This was just a prominent example. (This was actually supposed to be an answer for a different toot.)

can,
@can@haz.pink avatar

@SenseException I think one issue is that PHP doesn't have a handy way of mass assigning properties when constructing the object:

C#:
new StudentName
{
FirstName = "Bobby",
LastName = "Tables"
};

PHP :thisisfine:
$student = new StudentName;
$student->firstName = "Bobby";
$student->lastName = "Tables";

or you have to pass them in via constructor arguments in the correct order and it's just a pain. I always try to use them, but every time I find it inconvenient because it's so cumbersome.

afilina,
@afilina@phpc.social avatar

@can @SenseException You have named arguments in PHP, so the order doesn't matter.

can,
@can@haz.pink avatar

@SenseException (thinking about it, now with named arguments and constructor property promotion, it may no longer be an issue. Haven't connected the dots on these two features, it seems.)

Crell,
@Crell@phpc.social avatar

@can @SenseException Named args + CPP = we have easy structs/value objects now, get with the program. 🙂

can,
@can@haz.pink avatar

@Crell @SenseException isn’t that exactly what I just noticed myself? 🤔

Crell,
@Crell@phpc.social avatar

@can @SenseException Yes, I am confirming that your realization is correct.

klabauterjan,

@SenseException Everyone around me: still uses arrays instead of value objects 🥲

  • All
  • Subscribed
  • Moderated
  • Favorites
  • php
  • kavyap
  • InstantRegret
  • ethstaker
  • DreamBathrooms
  • mdbf
  • magazineikmin
  • thenastyranch
  • Youngstown
  • tacticalgear
  • slotface
  • Durango
  • khanakhh
  • rosin
  • everett
  • Leos
  • vwfavf
  • normalnudes
  • osvaldo12
  • cubers
  • GTA5RPClips
  • cisconetworking
  • ngwrru68w68
  • anitta
  • provamag3
  • tester
  • modclub
  • megavids
  • JUstTest
  • All magazines