---
title: Programming by wishful thinking
url: "https://aaronfrancis.com/videos/programming-by-wishful-thinking"
path: "/videos/programming-by-wishful-thinking"
type: video
author: Aaron Francis
published: 2025-08-13
keywords: [trait pattern, reflection, laravel helpers, trait inspection, psr-4, reflection class, model scanning, abstract methods, trait-provided scopes, artisan command, class resolution, interface design]
youtube_url: "https://www.youtube.com/watch?v=Qr1lYJmBTWI"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Programming by wishful thinking

## Chapters

- Programming by wishful thinking
- The 'ModelsThat:useTrait' idea
- The hasEmbedding trait pattern
- Abstract methods for embeddings
- Applying the trait to different models
- Trait-provided relationships and scopes
- Writing the embeddings:create command
- Starting the helper: modelsThatUseTrait
- Scanning files and resolving class names
- Converting paths to PSR-4 class names
- Using ReflectionClass to inspect traits
- Filtering models by trait usage
- Returning class names as a collection
- Verifying the helper works
- Next step: creating

## Transcript

**Aaron Francis:** I tweeted this the other day, and the, the point was I get a lot of joy out of writing Laravel. That is true, but what people really honed in on was models that use trait. They were like, "Ooh, what is that? How do I make that? Is that a part of Laravel?" No, I just, you know, I wrote models that use trait, and I thought, "That sounds pretty good. Let me see if I can go implement that now." So I wrote this part first and then the implementation later.

**Aaron Francis:** That's programming by wishful thinking. I wish this existed, so I'm gonna write it, and now let's go figure out how to do it. So we are going to do just that. I've left the old one around, models that. That's the old one. We're gonna do it from scratch. So whenever I'm programming by wishful thinking, I just write what I want to see, models that use trait.

**Aaron Francis:** That is what I want to use. And what we're doing here, it might be interesting for you to know, is, um, I have a trait called Has Embedding, and this can go onto any model that has, um, the, that has a vector embedding underneath. And I really, really like this pattern. So if we go to, uh, if we go to the article model, you'll see that an article uses Has Embedding.

**Aaron Francis:** So this, uh, this model right here has something that, uh, could be turned into a vector embedding. In the case of an article, it is the content of the article. And in this trait, we have a few abstract functions, so this is really nice, right? So if you're gonna tell me that an article has an embedding, uh, I need a little bit more information from you.

**Aaron Francis:** I need to know, all right, when does the article need an embedding? Well, probably in the case of an article when the content is present and an embedding doesn't exist. And then the second thing I need to know is what text should I generate the embedding from? So if we hop back to article, uh, if we hop back to article, you'll see that text for embedding is the title and, uh, concatenated with the content.

**Aaron Francis:** And if we say, uh, needs embedding, where it doesn't have an embedding already, and the content is filled out. So what we're doing is we're deferring to the model. This trait says, "Yes, you have an embedding, but I need more information. Model, you must implement those methods. You tell me when you need an embedding, and you tell me what the embedding should be created out of." Because, uh, for an article, it may be one thing, but if we go to...

**Aaron Francis:** I have a model called topic, and that has an embedding, and this is just simply, you know, where it doesn't have an embedding. And the text for the embedding in this case is in a column named text, not in a column named content. So I like this pattern, right? I like putting, uh, this trait on models and then implementing those abstract methods.

**Aaron Francis:** If we go back to the Has Embedding, you'll see we get a few nice things on every model. We get, uh, the related. We get, we can look for related embeddings here just by, uh, comparing the embeddings using Postgres. And then, uh, we have the relationship defined on the trait as well. And so what we have is, uh, basically a polymorphic relationship here, and the source just goes back to either the article or the topic.

**Aaron Francis:** Now, in this command here, embeddings colon create, create vector embeddings for models. So what I need to do is I need to gather all models that have embeddings, see if any of them need embeddings, and then create those embeddings.

**Aaron Francis:** That's what we're trying to do in this class here. And so to gather up all of the models, this is the signature that we're gonna use. So models that use trait, and then we're gonna pass Has Embedding class through, and then we'll say each, and then what we want here is we want a string that is a fully qualified class name.

**Aaron Francis:** And for now, let's just dump the class out. So models that use trait. What we're gonna do first is let's come out to... Where should we put this? Let's put this, I think, in support. Yeah, that's where models that old lives. So we'll put this here, models that. So models that.

**Aaron Francis:** Okay. What are we gonna do? Well, we, we know that we definitely need... We can go ahead and import this now. We know that we definitely need a, uh, use trait, models that use trait, so we'll go ahead and implement that. So we have public static function use trait. Yep, that's good. We can mu... It is a class.

**Aaron Francis:** I'm gonna name that trait, though. Now what? Okay, so what, what I like to do here is I like to, um, I like to prove one thing at a time. That's how I program. That's how I teach. Prove one little thing at a time, make sure we're working with a solid foundation, and then move on to something on top of that. So the first thing that we can prove is we can say, um, let's just say app path models.

**Aaron Francis:** Let's just see if we can find all of the models or, um, at least the place where the models live. So if we run this, we'll say LiveOS app models. Off to a pretty good start. We know that the models live there. Now we need to get all of the files in that directory. So we'll say file, all files, at path models.

**Aaron Francis:** How does that feel? Okay, we're getting somewhere. Um, all right, so we're getting, we're getting a collection of SPL file info. Cool. I can work with that. Um, it's actually an array. So we'll say files equals that, and then we will collect those files, and, uh, let's map over them.

**Aaron Francis:** And cool, it picked it up. SPL file info, that's good. Now what do I want out of there? I probably just want the path, so let's do this. DD file, file get- Path name. Is that right? I think... Yeah, path name is actually what I'm after, 'cause that includes the filename.

**Aaron Francis:** And there we go, article.php. That is correct. Now, we will, we will clean this up. You, you may look at this and think, "Well, you should do..." We will clean it up. The first goal always is to get it working. Prove one thing at a time, get it working, then come back and clean it all up. What we need to do now is we need to turn this filename into a class, which, eh, that's pretty easy.

**Aaron Francis:** We're gonna use Laravel's, uh, string helper here. So we'll say string after. We want the part of the string that is after the app path, app path. And not models, just app path. And can we DD this? I think we can. We will need a semicolon. Okay, cool. So we get models, article.

**Aaron Francis:** That's good. Now we want, um, we want before the dot. So I just, I just know what PSR-4 is, and so I'm just turning it into that. So we have models, article, and then we need to prepend app. That is my, uh, app namespace, and I know that. And then we need to replace, we need to replace, what is it, forward slash with, uh, backslash, and that escapes.

**Aaron Francis:** So we need to put two there. App mo- models, article. Okay. And this will be the ultimate test. So we'll say value and class equals, and then, uh, the ultimate test is class, uh, class exists. That class, did we construct it correctly?

**Aaron Francis:** We did. Okay. So now we have, uh, we've taken the models directory. We've gotten all the files out of it. We've turned them into classes, and what we're gonna do next is we're going to create reflection classes out of them so we can start introspecting those classes just a little bit. We're gonna keep going with this kinda, like, dirty style here. W- we'll, we'll clean it up in a second.

**Aaron Francis:** So let's just overwrite this and say class equals new reflection, class of class, and then we're gonna do this. Class get_trait_names. Cool. And if we dump that out, we see, hey, there's our friend, HasEmbedding.

**Aaron Francis:** So, uh, we're using reflection to peek into that class and figure out what all of the traits are. Now we can do this. Um, if in array, uh, trait get... Yeah, that seems right. So the needle comes first, then the haystack. Um, let's do... Actually, hmm, th- now might be a good time to clean this up a little bit because, uh, this feels very much like a filter reject situation.

**Aaron Francis:** So let's do it like this. Let's do, uh, we're just gonna return the class here, and we'll take this guy, and you're mine now. So we're gonna map, return the class, um, filter. This is, now this is just a string of class.

**Aaron Francis:** And we only want to include, so if not class exists, return false. So that will reject any, there shouldn't be any, but that will reject any ones that don't exist. We need to bring this into scope here, and I don't think we actually need that in scope up there. Now format that.

**Aaron Francis:** Now, instead of an if, we can just return, and I think that gets us pretty close. Let's check. So we're filtering. So we're gonna remove some things. If it doesn't return true, we're gonna remove it. So if the class doesn't exist, brr, get out of here. Then we create a reflection class, and we're going to return if that trait is included in trait names, return true.

**Aaron Francis:** So we should be left with two models here. Hey, look at that. Article and topic. So it worked. We're done. Um, we're not quite done because I don't ever like to see sparsely populated, um, non-associative arrays? What do you call those? Um, I don't like to see sparsely populated arrays when you're using, um, integers as keys like that because that gets real wonky when you turn it into JSON.

**Aaron Francis:** So after every time I filter on a non-associative array, and that has a name and that is escaping me. God, what is the name of that? I cannot remember. Um, I just run values to re-key them. So now we have zero, one. And frankly, that's it. We're done. Um, this is not, this is not very extensible, right?

**Aaron Francis:** This is not very flexible. It's not very extensible. It works, and that's step one. I mean, that's pretty good step one. Um, so we're gonna go back to the command that we're working with and show you how that actually works, and then we're gonna create a, we're gonna create a reflection collection that is way more flexible and a lot more powerful and a lot more fun.

**Aaron Francis:** So I don't know why I did this in my code and I'm giving you the good stuff, but hey, it's your lucky day. Coming back to the command, we see one error, and that is, I think we left... Yeah, we left that wrong. So this should be a collection because it should return a collection. So yep, we collect them, we map over them, do some stuff, filter out the bad guys and return the values.

**Aaron Francis:** So yep, there, cool. We're good. Now we can run the command again, and we should see two dumps here, article and topic. So article and topic is right, and then all we would really do is just do this. We would do class query, and we know, like, we know- Inside of this each that we are dealing with a model that is using the HasEmbedding, and so we can use these things pretty safely.

**Aaron Francis:** So we'll say, um, uh, needs embedding 'cause that's the name of the scope, and then we'll just limit to... Eh, we'll limit to five, and then we'll get them, and then we will each over them. And then in here, uh, we'll just say, "This is an instance of model," and let's just dump model ID and, uh, we can do get class model.

**Aaron Francis:** So let's just see what happens inside of the body here. There we go. Yeah, that works. Um, okay. So we have an article of fourteen, fifteen, sixteen, seventeen, eighteen that all need embeddings and topics of thirteen through seventeen that need embeddings. And then once you're inside of here, you're still guaranteed that this is a HasEmbedding, so you can say, I think it's text for embedding, I think.

**Aaron Francis:** And so if we dump that out, that's gonna be a lot of text, but that's fine. If we dump that out, you'll see, ooh, there's a lot. Okay, cool. That's all the text for the embeddings that we would send off to OpenAI and get the embeddings for. That's for a different video. What we're gonna do now is we're gonna make something more powerful and more awesome, and we're gonna call that a reflection collection, which just sounds really nice, a reflection collection.

**Aaron Francis:** So let's program by wishful thinking there too. So long to that one. Thanks for all the fish. Now, let's program by wishful thinking and say, "Reflection collection," uh, "from directory." That feels nice to me. And instead of, uh, instead of sh- they should all just be in the app path, right?

**Aaron Francis:** So we'll just say models. Reflection collection from directory models. That already feels pretty good. Um, I need a way to say that this is not an abstract class, and we'll say, um, is instantiable. Is instantiable. Uh, uses trait, HasEmbedding.

**Aaron Francis:** Um, I think that is all, and then I think at the end, I want to get class names. That feels correct to me. What I like about this is you could do something like, um, HasProperty. You could say HasProperty. Uh, I, I don't, I don't know what property it would have.

**Aaron Francis:** And then you could use, um, I think there's a reflection, reflection property, uh, is public. Yeah. Cool. So you could do something like this. So you could grab all of the models in a directory. Um, in this case, they're models. We could grab all of the classes in any directory and say, "I need it to be instantiable.

**Aaron Francis:** I need it to have this property. I need it to use this trait. I need it to not have this property. Give me all of those class names." It's kinda cool. We're gonna stick with just these two for now. So I'm gonna take you and come out here, and we'll make a new class called reflection collection, and this is going to extend Laravel's collection, so we get all of that nice stuff already.

**Aaron Francis:** We'll do public static function. Public static function. What did we call it? From directory, and then we'll do a string directory. Yeah. And okay. So I think we can go back, and we can import that now. Cool. Import class.

**Aaron Francis:** And we need IsInstantiable and UsesTraits. Actually, there's three. And, um, I don't know that GetClassNames is right. Um, we'll see. That feels close, but I'm not sure that that is right. Get... I think I really want, like, map into class names or something like that. Public function.

**Aaron Francis:** Okay. Um, why did that go so... Oh, I put it all, I put it all in that, uh... There we go. Oh, come on. Okay, here we go. Very good job, Aaron. From directory. So we're gonna pop out to models that, and we're gonna take...

**Aaron Francis:** We're just gonna yoink all of this and modify it a little bit. I think it makes sense to just turn these into reflection classes just right here in the, um, right here in this static constructor. So we're not gonna return. We're going to collect, um, and in fact, I don't think this is the right place to do that.

**Aaron Francis:** We'll get to that in a second. So these are all... Yes, yes, that's fine. Um, prepend app. Yep, yep, yep, yep. Now we don't wanna filter anything out yet. First, we need to map over it. Um, if the class... We, we can, we can filter up here. We'll do one filter.

**Aaron Francis:** Filter function. Um, I don't... I still don't know how this would happen, but that's okay. So if the class doesn't exist, um... Oh yeah, these are classes now. If the class does not exist, then just bail out. Um, filter, return.

**Aaron Francis:** This is better. Return the class does exist, then we can make this a short one. Okay. So we're gonna filter down to only ones where the class exists, and now we're in a good, safe spot here, so we don't get any errors. So we're gonna create a new reflection class, and I think We can just...

**Aaron Francis:** This is getting a lot simpler. Uh, arrow function, yes. And because we filtered, we do need to do values now. This is good. I don't think this is the best because we might wanna, um, we might wanna do something like this, from directory models, add directory, um, I don't know, uh, not models.

**Aaron Francis:** Well, just add directory support, right? Um, so we need to do it like this. Public function, not up there. We should do it down below the static. Public function, add directory, string directory. Yep. And then we're gonna take this and put you down here, and we'll say that files equals that, and this...

**Aaron Francis:** I think there's a merge. Merge? Yes. Files. Uh, files all. Okay, that feels closer, so then this simply becomes return new static. Um, add directory, directory.

**Aaron Francis:** That feels good. I like that better. Add directory. I mean, is it add directory or with directory? Reflection collection from directory models with directory, add directory support. Ah, I kinda like add directory. Okay. Directory, directory, directory. This is getting a little weird.

**Aaron Francis:** So now if I do DD, what do we get? Um, okay. So yep, yep, yep. Models, models, models, models. Lots and, lots and lots of models. There are a lot of models in this project. I think I've got, like, 20 or 30 models. Um, okay. So I think that is...

**Aaron Francis:** I mean, do we have any, uh, support? Yeah, no. All right, it's good enough. I think we got it. I think we got it right 'cause we do see all the models. We need to implement is instantiable. So we're gonna return this filter, this filter function, and we know that we're dealing with reflection classes.

**Aaron Francis:** So class is instantiable. Cool. I'm liking this a lot. Um, so first return is instantiable. Yes, and then we can do use trait and trait. I'm not gonna verify that that cla- that trait exists because if it doesn't, we won't get an error.

**Aaron Francis:** It's just gonna say, "Yeah, none of those classes implement that." Um, let's turn this back into a long one. We can go back to models that and get this one. And we'll say in array trait, and yeah, we can make that short again. And I actually think get class names is correct because this needs to return an array, because we can no longer operate, we can no longer use

**Aaron Francis:** these methods once we turn it into class names, because these are expecting reflection classes. So if we do, is instantiable, uses trait, uh, let's just DD here, and wake up.

**Aaron Francis:** Boy, those are long. Um, ah. Da, da, da, da, da. It's just a lot of properties on here, isn't it? So sometimes when I get to this, I kinda like scroll up just a little bit. There we go. Reflection class, boom.

**Aaron Francis:** Models article. That's good. Um, what we didn't do is... Do I want to call values? I don't think that's within my right to call values right here, because they may have keyed by something else, and if I call values, I'm blowing away their keys.

**Aaron Francis:** Uh, so I don't think that... I don't think I'm allowed to do that. That feels unsafe to me. So in this case, I'm going to get class names. I'm going to map reflection class, get name. I think get name is right. We do need to turn that into an array here in a second. Um, ooh, do we turn it into an array, or do we turn it into just a base collection?

**Aaron Francis:** I think we turn it into a base collection. Um, yeah. That's on me, dog. All right. Let's turn this into a base collection. Hey, cool. That's exactly, that's exactly what we're after. Um, so now I can do this because I'm in charge out here.

**Aaron Francis:** Hey. Ah. Look at that. That's pretty cool, and this reads so cleanly. So reflection collection from directory models is instantiable, uses trait, give me the class names, get rid of the wonky keys, and then obviously DD. Which I think means we could do the each again because, yeah, r- turning it into an array was never the right idea.

**Aaron Francis:** This is, this is much better. 'Cause then we can do string class, and we'll do just dump class there. And yeah. Hey, not bad. This is pretty good. This is one of my, this is, this is one of the great joys of programming. You get to invent the universe that you then occupy.

**Aaron Francis:** And so if you start by creating, um, the nice readable interface and then going and figuring out all the, the ugly stuff underneath it, you get to use that nice, beautiful interface. You just get to make it up. You just get to invent it from whole cloth. It's quite nice. Y'all are free to turn this into a package, but I want to encourage you that when you see stuff like this on Twitter, just think, "Yeah, maybe I don't need a package.

**Aaron Francis:** Maybe I can just write it myself." I wanna show you one more place where I am using this, and that is in an old project here. You'll see, um, properties init from query string returns a reflection collection. So the first thing we do is we collect the properties, and it's just, uh, making a reflection collection out of the properties of, um, out of a, a certain class, right?

**Aaron Francis:** And so we collect all the properties, and then we filter if it has any annotations that match sync query string, we include it, right? So you could put, you could put a bunch here, and if it matches any of those, include it. And we want to include any modifiers. So what we're saying is, if it's annotated with this annotation and it is public or it's protected and it is not static or read-only, then that qualifies as the properties that should be

**Aaron Francis:** init from a query string. And you can see properties init from state. So this is properties, uh, properties for state, which is right here. So again, we're collecting the properties. We're including all modifiers, so it must be public. We're rejecting any modifiers, so it must be public, it cannot be static, and it cannot have the annotation of server only.

**Aaron Francis:** And then we say properties init from state. We're also rejecting any annotation of read-only. So in this case, we're, um, taking a previous collection and adding one more thing on top of it. So this is, uh, this is just another, this is just another collection class th- that is pretty straightforward. Um, but this is another example of where you can invent the interface or the, uh, the affordance that you wish existed, and then you get to live in this blissful, peaceful world of using

**Aaron Francis:** the beautiful code that you yourself wrote and understand at a fundamental level. Please let me know what you think about this. If you wanna turn it into a package, that's fine. Leave a link down below if you do. I would love to see it. Until the next time. See ya.

---

## Related on aaronfrancis.com

- [Finding a brand for Database School](/newsletter/finding-a-brand-for-database-school) *(Newsletter · 2025-08-15 · shared: reflection)*
- [Shipping SQLite to Lambda (with Laravel Vapor)](/writing/shipping-sqlite-to-lambda-with-laravel-vapor) *(Article · 2023-12-19 · shared: artisan command)*
- [Ensuring consistent polymorphic relationships with Laravel](/writing/ensuring-consistent-polymorphic-relationships-with-laravel) *(Article · 2020-10-26 · shared: abstract methods)*
- [Living the Quiet Life](/podcast/living-the-quiet-life-4a4031ee) *(Podcast · 2015-12-18 · shared: reflection)*
- [I built a GUI in the terminal (with PHP)](/videos/i-built-a-gui-in-the-terminal-with-php) *(Video · 2025-01-31 · shared: artisan command)*

## Relevant Resources from Aaron Francis

- **[Solo](https://soloterm.com)** (overview on site: [/software](/software)): All-in-one agentic development platform and terminal workspace built by Aaron Francis for orchestrating processes and AI coding agents.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `trait pattern` ([T-Z](/llms/keywords/t-z.txt)), `reflection` ([Q-R](/llms/keywords/q-r.txt)), `laravel helpers` ([K-L](/llms/keywords/k-l.txt)), `psr-4` ([O-P](/llms/keywords/o-p.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
