• Perks with a Purpose!

    Introducing Administrata+ and Pro—because supporting your admin journey and our community should go hand in hand.

Php 8.4 has been released: What’s new (2 Viewers)

You probably can’t implode them.

If I did this in Laravel (or any other equivalently modern OO-first environment), my array internally isn’t a list of names. It’s an array of author objects. (It might even be something resembling Laravel’s Collection helper which definitely is not an array)

You cannot just implode a list of objects unless you’ve gone out of your way to seediness specific methods to say to PHP “if someone tries to use this object as a string, do this with it”

But this is really more a case combined with the first thing: you probably have a book object with its array of author objects and you might want to let other parts of the code have access to the real array, but they can have a pre-made digest format.

Really it’s about making it easier to define ways to get at the data an object already has. You’re thinking about my example solely in the context of that example, when that’s really not the intent of the feature.

OK, better example: I have a system that handles money. Everything is done in fixed precision for accuracy. I have an object that represents the product with its price, in home currency. What I also will need from time to time is the price of that product in Stripe terms. For those not familiar with Stripe’s API, a product that is USD 149.99 is not sent to Stripe as $149.99 - rather it’s sent as 14999 US cents.

Since I know I’m going to need the price for Stripe on various things, I define a new property in the current longhand way that says “get me the current actual price, then convert it to what Stripe wants” meaning that I have my product and can ask for $product->price_regular and get its price for normal purposes as well as ask for $product->price_stripe and get it ready formatted for Stripe without having to duplicate the logic everywhere I would otherwise need to do the conversion. I also never want to allow anything to override this value.

Similarly I can define $product->tax_amount as a property where the rest of the system can just trust that the product knows how much of its own price is taxable and therefore how much tax is liable.

What we’re really saying here is that in the pre 8.4 world we can define properties that are either read/write or not accessible (with no easy gated update method) and any additional behaviour is much more longhand, and that in 8.4 we can very easily define any type of attribute we like and make it very easy to define “if you get asked for this, here is what you do and if you get asked to update it, here is what you do” including “you can look but you can’t touch” attributes and entirely derived or computed attributes that are made from something else.
 
  • Like
Reactions: frm
Lmao, I was about to ask the same thing. I’ll have to read Pete’s reply after work today, as he definitely took his time to explain again, and I’m running out of time. 😋
Without being too much of a brown noser here, I've always looked to Pete for programming advice and thoughts. He's good!
 
I try to be helpful, because I’ve long been a believer that this stuff isn’t magic and that if people want to learn, I’ll try to share what I know - I am just mindful my communication and explanation style don’t always translate well.
 
You cannot just implode a list of objects unless you’ve gone out of your way to seediness specific methods to say to PHP “if someone tries to use this object as a string, do this with it”
That is the entire point of __toString() :) no going out of your way and its not seediness :)
 
I have no memory of what I intended to write there, but it was very definitely not that and I got autocucumbered again.

As for the example, again that's a limit of my bad example, though if you have a book with a collection of book authors, you don't want to implicitly convert the book to a string (and if you did, you'd probably want to convert the book's title instead), but you can't overload an array with __toString() which means you're either writing a custom accessor (which is really all the new get/set syntax is) or you're creating a collection class solely for the purpose of being a collection whose default type-cast to string is to iterate over all the members, coerce them to string, and implode the rest.

For me such a collection would be oddly specific and I'd probably just roll it up into a function on the book object anyway, but that just leads me back to having a tidier version on the class as a computed property.

(I have written custom collection classes, e.g. the one project at work has one, but it's there specifically as a form of optimisation where I need to work on the same collection twice over, doing different things the two times but I don't want to fresh-load the collection in between, so it's really a collection with a specific deep clone function that manages resetting state, and applying specific transforms to every member of the collection)
 
@Arantor, man I wish you would join the Axleus Discord. Got sooo many cool things that are upcoming. Component based framework development with a comparable developer experience to laravel.
 
That’s one heck of a tall order given that Laravel is more than 10 years in, has a massive first party ecosystem that covers dev environments, managed servers, serverless and soon private cloud, and that the code ecosystem includes pre-built auth flows including 2FA, SSO with OAuth, websockets support, Stripe wrapper, first party dashboard components, and so much more.

Not saying it can’t be done but don’t underestimate how good the Laravel DX is given how much of an ecosystem it has, and the head-start they have.
 

Users who are viewing this thread

Back
Top