Larry Garfield

Larry Garfield

Mar 19, 2024

Scott Keck-Warren Larry Garfield
In this video, Scott talks to Larry Garfield about his talks at php[tek] 2024 and the open source projects he's created and maintained for the community. Links: Larry's Blogs - https://www.garfieldtech.com and https://peakd.com/@crell Serde - https://github.com/Crell/Serde AttributeUtils - https://github.com/Crell/AttributeUtils Tickets to php[tek] 2024 - http://tinyurl.com/tek-2024-tickets 00:00 Intro 01:12 Why Should People Never* Use Arrays 05:06 Type Problems 07:48 Preventing Bad States 09:13 php[tek] 2024 10:17 PHP attributes let's get meta 17:59 Anything else about the talk people should know? 20:42 Outro and plugs

Transcript

Scott Keck-Warren Scott Keck-Warren 0:04
Hello developers and welcome to the PHP Community Corner where we have conversations with members of the web development community. I'm your host, Scott Keck Warren, and on today's show we're talking to Larry Garfield about his talks at PHP Tech and some of the work that he's done inside of the, the core PHP language. Larry Garfield has been building websites since he was a sophomore in high school, which is longer than he'd like to admit. Larry is currently a staff engineer at LegalZoom, but before that he was the Director of Development Experience for Platform.sh and a longtime Drupal contributor and consultant. He led the Drupal 8 Web Services Initiative that helped transform Drupal into a modern PHP platform. Larry is a member of the PHP-FIG core committee and has authored several books on PHP development, including Thinking Functionally in PHP and Exploring PHP 8.0. Larry holds a master's degree in computer science from DePaul University, and he blogs both at garfieldtech.com and peak.d.com/krell. I will put links in the video description for that. Thank you, Larry, for finding some time to talk to us today.
Larry Garfield Larry Garfield 1:07
Hello, world.
Scott Keck-Warren Scott Keck-Warren 1:08
So you're one of the speakers at PHP Tech this year. You're giving 2 talks. The first one that we're gonna talk about today is the Never Use Arrays. Can you tell us a little bit about that?
Larry Garfield Larry Garfield 1:18
So, Never Use Arrays is a talk I first put together right before the pandemic started. So most of the times I've given it have been remote. But those who know me and know my work know that I'm a longtime fan of formally defining your problem. I love the type improvements in PHP in the last several years. I hate using arrays as though they were a data structure because they're not. They are what you use when you don't have a data structure. But formally defining a data structure right now in PHP is so easy compared to what it used to be that there's really no reason to use arrays anymore for anything beyond a list. And we have better options in most cases. So what I go through in the talk is, you know, here's all the places that PHP developers traditionally will use arrays, and here's all the things that are better to use instead. And by better, I mean more type safe. I mean allow you to structure your code such that certain error conditions simply cannot exist. I mean error handling that becomes easier and more centralized. Code that becomes more self-documenting. code that your IDE will pick up and autocomplete things for you, which arrays don't do. Some IDEs and some tooling will give you a little bit, but nowhere near as good as just use an object. And just use an object is so easy now, especially with PHP 8, that there's really no reason not to. So that's kind of the idea. It's a It's a talk about arrays, but it's really a talk about data modeling in a sense, and how to think about data modeling. And just when you do formal data modeling on things, and by formal I mean you just define a class, you don't need to get more formal than that, you start to realize, oh, I can now move my code around this way, and that makes things easier. And I can move my code around this way, and that makes things easier. And then this way, and I've got a completely different API structure now. That is way better than just having a big blob of arrays somewhere. So that's, that's the general gist of the talk.
Scott Keck-Warren Scott Keck-Warren 3:41
Okay. That sounds really fascinating because, because from my perspective, I'm a big proponent of like not even using like basic types anymore, right? Like anytime that you can have an object that you can tell your compiler, or you can tell your IDE some way that what it is, it just automatically is going to help you type faster code. And then we can run it through like PHPStan. So it can go, oh, hey, you made a mistake, right? Like immediately you just go, oh, there's a problem here. As opposed to you like spending 15 minutes trying to figure out why you misspelled like a property name or something.
Larry Garfield Larry Garfield 4:11
One of the teaser, one of the examples I use in the talk is if you have a field in some record called canceled, how do you spell that? Is it one L or 2? It depends on what country you're in because in different countries it's spelled differently. So if you have an array where one of the keys is called cancelled, is it 1 or 2? How do you know? You really don't. You kind of have to guess, and maybe you'll guess right. With an object, the IDE will tell you. You'll type ca and it'll fill in the rest for you. You don't even have to think about it. That's the kind of low-level benefit that using real data structures provides, is that Um, just certain problems just don't exist anymore. And that lets you build up to doing even better things at a higher level.
Scott Keck-Warren Scott Keck-Warren 5:06
Yeah. And the one, the one, my favorite example always is like, if you're dealing with currency, like if you have, it's just like some amount, is it, what is the type it's in? Is it, you know, if it's, so it's, is it US dollars or is it pounds or, you know, is it PHP and like, is it in cents or is it in dollars or is it, you know, is it a float? Is it an integer? And so like all those like problems just are like completely gone. You don't even have to think about them.
Larry Garfield Larry Garfield 5:29
Currency is one of the common examples there for that. But there's a lot of others, like distance. Is it in inches? Is it in centimeters? Is it in kilometers? Is it in light years? You know, just an integer doesn't tell you that. And God forbid an array is not going to tell you that. But if you define it as a miles object which has one value in it. You can build up some stuff around there, like a toString method or whatever. And now you can say, oh, this is a number that is stored in miles, and I know that. It is not possible for me to get it wrong anymore. The engine will complain if I do. And if I want to deal in both miles and kilometers, cool. I have another object, kilometers, and I have logic to convert between the two. Because that's known standard logic. And now I can say, all right, this part of the system is using kilometers, this part is using miles, and I'm not going to get confused because the types will tell me if I get confused. One of the common phrases used in the functional programming world and the advanced typing world these days is make invalid states unrepresentable. And, you know, or variations on that. And I absolutely support that approach. You know, if— you know, if— Common example here. If you're designing, if you're modeling an airlock on a spaceship or a submarine, then you don't want the inner door and the outer door to both be open at the same time. This is bad. So don't put in error checking to make sure that doesn't happen. Define the structures such that there is no way to actually get to that state. The types— the type system itself just doesn't let that exist. And then you can't accidentally end up in that state because the program doesn't have a concept of that state. There's a lot of examples of that kind of thinking where if you just think through the problem correctly, you can make error cases disappear by modeling your data correctly. I don't go that deep in this talk, but I do talk about that a little bit in my book on thinking functionally in PHP.
Scott Keck-Warren Scott Keck-Warren 7:43
Which also would be a good, good, sounds like a good thing to pick up at this point. Yeah.
Larry Garfield Larry Garfield 7:47
I like it. That's,
Scott Keck-Warren Scott Keck-Warren 7:49
uh, I, that is actually like a really, um, interesting concept to just say like, well, you just can't get in this state. Right. Cause, cause a lot of times we're just like, well, how do I get my code to work? Get it to work. But you know, a lot of times, especially like I, I guess me specifically, I never go like, how do I prevent any errors from ever occurring? And I know like I've really gotten into value objects recently. And like, that's, that's like one of the benefits of value objects is it's like, well, you can never have a negative currency. You can never have a negative distance like that, that kind of like, you know, don't be in a bad state just comes built into value objects if you're doing it right. So that's a really great concept.
Larry Garfield Larry Garfield 8:27
And that's, that was part of the idea behind enumerations in PHP as well, where, you know, this value could be ascending or descending. There is no way to just represent anything else. Therefore, I no longer need to code for this might be something else. That use case just disappears. And now my code is correct and I can prove that just because my IDE isn't yelling at me. So, you know, different languages support that to different degrees. The more we can make PHP support that, the better everyone's code will be. I also touched briefly on generics, not that we're actually, you know, not actually getting them in PHP, but kind of how to emulate them again for data modeling purposes.
Scott Keck-Warren Scott Keck-Warren 9:13
Yeah, absolutely. So great. Everybody should go see it. Go see this, this talk. It sounds great. Or watch the video in the replay. So, but we're going to go to a quick break from our sponsors and then we'll be back again with Larry. Attention all PHP developers. Did you know that one of the best ways to improve your skills is to attend a conference? It's true. And have I ever got news for you because PHP Tech is returning for another year of leadership, expertise, and networking all in one event. It's a relaxing atmosphere for tech leaders and developers to share, learn, and grow professionally while also providing you the knowledge to solve your everyday problems. You'll learn things like building for the PHP command line interface from Steve Grunwell and the dark corners of the SPL from Omni Adams. Join us April 23rd to 25th, 2024, just outside of Chicago, Illinois. Or online, as all talks will be streamed live. If you can't make it during the conference itself, all talks will be available later for you to watch at your convenience. You'll even get access to last year's talks. Tickets are on sale now at tech.phparch.com or use my link in the description for a discount. And now back to the video. And we're back with Larry. So the other talk that you're gonna give at PHP Tech this year is PHP Attributes: Let's Get Meta. Can, can you talk to us a little bit about that as well?
Larry Garfield Larry Garfield 10:27
Yeah, so some background here. Attributes came out in PHP 8.0, and they're a wonderful new feature. I'm a big fan, but the API for them is fairly basic by design. You know, the internals team wanted to keep it simple and not get too complex with it and build out, you know, specific use cases. So it's fairly rudimentary. And my previous job, I actually worked for TYPO3 full-time as a staff engineer working on the TYPO3 CMS. And I was trying to rebuild our config system because it was a big giant blob of arrays that are global. And that's how everything was driven. And as we just discussed, that's not good. So, all right, I want to turn big blobs of arrays into well-defined class objects in a backward-compatible way. That really sounds like serialization, or really deserialization. Okay, so let's build a deserializer. And after looking at some of the existing ones, I found none of them did what I needed them to do. So I wrote my own because, you know, I tried to do the open source thing first. And made the decision early on that it would be driven entirely by attributes. It was written for PHP 8.1, and so attributes were available, read-only properties were available, all this stuff is just there. And so I figured, all right, I want to do some more fancy attribute stuff than PHP provides. I want to just wrap up some of that functionality. Okay, that's another library. So what I ended up building was actually 3 libraries. One was a small functional programming utilities library. We're not talking about that. One is AttributeUtils because I couldn't come up with a better name and it kind of stuck. And then one is Serdi, which is the serialization library. I talked about Serdi at Longhorn PHP last year. That video is available, I think, from the Serdi project page. But easily half of its power comes from AttributeUtils, which is a library that you can use to parse attributes in your code in a very automated fashion. And so it can do things like give me all the attributes of this hierarchy on all the properties of this object, bearing in mind this context. So I can have different attributes that apply in different contexts. And that's one command. That's just one method call to do that. So I could do things like, all right, get me all of the serialization directives in this object, and if one's missing, use these defaults. And, you know, here, use this to control what the defaults are, and then also use reflection to pull information out of the source code, like the type and the default value, that is not in the attribute, combine all of that together into an attribute object, and I now have a plain object I can just pass through my system. It's just a plain read-only object with public properties that came from the attribute system, and that can drive all of the other logic. So default handling, scopes, if you've serialization groups in Symphony, same idea, but this works for any attribute. You can have attributes that kind of roll up into each other easily. I mentioned reflection, inheritance. By default, attributes don't really do inheritance. You can opt into inheritance with this. So it's just really the Swiss Army knife of attribute handling for PHP 8.1 and later. And I built it for serialization, But the exact same logic would apply to an ORM. The exact same logic would apply to, um, you know, a simpler serializer, um, you know, for a routing system, for anywhere that you're parsing some standard attributes. This will make it easier and more powerful, uh, and also faster in some cases because it's designed to be easily cacheable. So, you know, you do an analysis, you just have it wired with a cache object in front of it, that gets cached out to wherever you want. You never have to actually do the reflection logic again. So it ends up being really fast in practice. Because it's all static analysis, you can also do a lot of things ahead of time to just warm the cache. Discover all registered event listeners in my system. That process could be long. But it just results in some very simple small objects with a couple of public properties, because that's all attributes are. And then you dump that to disk in some optimized fashion, and you now know everything about your entire system. You don't need to do any introspection later. A lot of this logic exists in various places already in various frameworks, but this takes all of that power and capability bakes it onto attributes and makes it available for basically anything. So this talk is kind of a commercial for this library I built at TYPO3, which TYPO3 ended up not using, but it's open source. So yeah, we can still have our cake.
Scott Keck-Warren Scott Keck-Warren 16:07
I always love this about the open source community where you said, well, here's a problem and I solved it and here's how I solved it and other people can use it. And So now you're just like making that available, which is really helpful for the community as a whole, obviously.
Larry Garfield Larry Garfield 16:19
Yeah, I have a number of people who are already using serde. I know this because they are filing feature requests. So they're therefore also using attribute_utils. I would love to know if someone is using attribute_utils outside of serde. It's designed so you can. I had some experimental projects where I was. Actually, I am using it in the next version of my event dispatcher, Tukio, where I'm switching the registration mechanism so it's more attribute-centric. And that now uses attribute utils. That's not released yet, but that will be out.
Scott Keck-Warren Scott Keck-Warren 16:54
That's really fascinating. I'm always amazed by how powerful PHP 8 has become essentially. I'm like, I honestly, to be completely honest with you, am struggling with how I would use this in my thing. It's probably more for library maintainers and framework maintainers and that sort of thing, correct?
Larry Garfield Larry Garfield 17:12
Yeah, attribute_utils is something you're rarely going to use directly. It's something that you use to power something else. Like you use it to power your serializer, you use it to power your ORM, you use it to power your event dispatcher and so on. You can use it directly, But I have other libraries where I'm using attributes and my needs are low enough. I don't actually use attribute utils for that. I certainly can, but if I'm just looking up one single plain attribute and that's the end of it, it doesn't really— it's not really necessary. But when you're doing more advanced, more complex stuff, then it saves a ton of time and hassle. And quite frankly, I think some of the techniques I use in the code are quite impressive. I was very proud of that codebase.
Scott Keck-Warren Scott Keck-Warren 17:59
That sounds like a good talk. I don't know if I'll make it to that one. I think it's probably outside my scope of interest. But is there anything else that you want to direct our listeners and our viewers to before we let you go today?
Larry Garfield Larry Garfield 18:11
Well, as I mentioned, Serdi. So if you want what is, I would argue, the most powerful and performant serialization library in PHP today, Krel Serdi, it's on GitHub, it's on Packagist, all that kind of stuff. I'm also— I've been working on some RFCs for PHP internals. The latest one is on property hooks along with Ilya. And I'm hoping that passes. We're going to try to take it to a vote, I think, before PHP Tech happens, probably. So it may pass around the time of PHP Tech or something. So hopefully, but that again goes to How can we define our problem space? How can we make our type definitions more powerful, more robust, with less boilerplate? And that again goes to, you know, that whole, you know, if the code has gotten to this point and not crashed, I know everything is correct. And hooks are another step in that direction. So fingers crossed that passes. It's a long RFC, but that's because we're— dealing with all the little details to make that work.
Scott Keck-Warren Scott Keck-Warren 19:18
Well, it'll be interesting to see if that tech makes it into PHP.
Larry Garfield Larry Garfield 19:21
I certainly hope so. I also, if I can promote someone else's talk, there's another talk on enumerations at Tech that I'm going to be going to because I want to see what the presenter says about it or says about them, because he actually gave a presentation on enums several years ago and said, yeah, and we'll never actually get them in PHP. Challenge accepted. So I'm quite curious to see how that plays out, and hopefully we can get more features into enumerations in the future as well. Like, the next big one is algebraic data types, so being able to associate values with an enum instance, which is something that Rust and Kotlin and C# and a lot of other languages support. And that becomes— we're talking about making invalid states unrepresentable. That lets you say, all right, there's only 3 possible variants of this command. They have different associated properties, but there's only 3. And I can just define that in this much code. And now all that logic is there. So hopefully that's something that Ilya and I can get to in the next year or 2. Fingers crossed.
Scott Keck-Warren Scott Keck-Warren 20:33
Thank you for your time today.
Larry Garfield Larry Garfield 20:35
Yeah. Thank you. And I'll see you at the conference.
Scott Keck-Warren Scott Keck-Warren 20:37
Yeah. And again, it was nice seeing you. I know we've like met a couple of times, so it's nice seeing you again. I had to say another heartfelt thank you to Larry for all of his time today. And thank you for listening to our podcast. If you're not already a subscriber, make sure you subscribe with whatever your preferred method is so you can get episodes as soon as they're released. Maybe share this with a coworker so that they can learn as well. This podcast is available both as an audio version on the PHP Architect website, as well as a video version on the YouTube channel. This is Scott Keck Warren for the PHP Community Corner, signing off and reminding you to keep listening, keep coding, and keep reading.