An alternative to associative arrays in PHP

preview_player
Показать описание
👨‍💻 Learn Test-Driven Development with Laravel!

👨‍💻 Sign up to my newsletter and receive PHP, JS and Laravel news in a weekly-basis:

🎉 Party up:

Thanks for watching!
Рекомендации по теме
Комментарии
Автор

Nice video, Mateus! Quick joke for you-- why did the developer quit his job? Because he didn’t get arrays (a raise) 🙂

andyhinkle
Автор

Years of writing PHP, and i still learnt something new today, thanks lad.

QuintessentialDio
Автор

this has become a LOT more relevant since PHP introduced constructor-defined readonly properties. Creating value objects, DTOs whatever is now pleasant and elegant.

TotoTitus
Автор

i do this on last my project, for Entities in laravel implemented clean architecture. and very helpful for maintaince data return from services and repositories

david.arl
Автор

Honestly this is a great idea and allows us do treat some classes more like structs. Objects tend to be more efficient than arrays. As other people have pointed out, this is like a DTO but without the getter methods you typically see in DTO classes. Good stuff!

alasdairmacintyre
Автор

I would only add that, as a general rule, it should be understood that arrays are much more efficient in terms of memory and processing. Therefore, for any process that requires iterating over collections that can grow large, arrays should definitely be used, and objects should not be used at all.

For maintainability, architecture definitely matters. Given the above, as a general rule, I would use objects as a means of communication between software components within the defined architecture, but the implementations for handling large data collections should be abstractions that use arrays behind the scenes. Let's say that algorithms, which need to be implemented correctly from the start and rarely modified, should definitely be implemented using arrays.

mkGarf
Автор

Min 13:20 the class "UserSetthings" can be "final readonly class UserSetthings".

chechomancr
Автор

The timing for this video couldn't be better as I was literally talking about these exact points yesterday with my team. Great content man, kudos from Brazil! Also it was awesome to see u on Laracasts as well 🤩 Parabéns meu bom

brunoggdev
Автор

Excellent video! What font do you use? It's awesome.

josiasvelazquez
Автор

This is also what JS has... Array in JS is an Object with some additional symbols.

LewisCowles
Автор

This is all dependable on the context... good thing for complicated data but overkill for some simple data...
Generally the video is a good hint about what can you do in PHP as a programming language.
"Chose your 'weapons' wisely!"

easyvideott
Автор

Great video. I've been using this approach a lot lately. How do you work when you have an attribute in UserSettings that must be set after the object creation and need to use it inside the notifyUser function? When do you check if the attribute was set or not? Thanks so much.

marceloAK
Автор

Thats solid principles applied into php. Thats awesome!

LucasAlves-bwue
Автор

Hi! As I understood we use just plain php classes(like UserSettings) when we want to use it internally, probably withing one layer. And if we want to pass data from controller to service or other layer we use DTO, right?

ihorrud
Автор

Absolutely love to do this when possible for sure.

Thank you for making these contents. ❤

victoradeshayo
Автор

Thats is why they invent the DTO concept

josephmakram
Автор

Yeah, I do this all the time. Makes refactoring much less stresfull. Also, using Propel ORM for the sam reaoson. It allows you to change the database schema without having to worry that code will break.

Too bad worse ORM solutions like Eloquent are more popular than Propel. I guess PHP developers suck.

DavidSmith-efeh
Автор

I made a PHP “clone” of Pydantic for typed collections

stonedoubt
Автор

One (still) useful usage of array is a pseudo Tuple:

function doSomething(){
if (somethingWentWrong()){
return [null, new SomeBusinessException()]
}

return [SomeDto, null];
}

function someConsumer(){
[$data, $error] = doSomething();

//
}

A pseudo Result<T> monad, but until PHP had generic, Tuple will have to suffice

khangle
Автор

How will you handle additional variable in the settings? Do you have to update the UserSettings again? Keyword: dynamic settings

nelsonmelecio