Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
What Are Polymorphic Relations and How Do We Use Them with Eloquent?
Dec 19, 2016 @ 13:09:38

On the SitePoint PHP blog there's a new post from Younes Rafie looking at polymorphic relationships and how they're used in the Laravel Eloquent functionality to relate tables and entities to one another.

While I was working on an application for a client, I had to implement a new module that entails the following:
  • Users ask for a budget quotation for a certain task.
  • Every task has a location.
  • Professionals can subscribe to different zones.
  • A zone can be a region or a city.

Now, let’s neglect the core application and try to implement this single module to see what we can achieve here.

He starts off by scaffolding out a basic Laravel application, setting up the database configuration and creating the migrations for "Zones", "Regions" and "Cities". He looks a bit more in-depth at how the models were created and how to create the methods relating one to another. The relation goes "through" the zones handling, so they use the "morphedByMany" and "morphedMany" to tell Eloquent how to make the connection. He then shows how to use the relations in your code and some of the collection helper methods to make it simpler to get just the data you want.

tagged: eloquent polymorphic relation introduction tutorial

Link: https://www.sitepoint.com/what-are-polymorphic-relations-and-how-do-we-use-them-with-eloquent/

Brandon Savage:
Using objects doesn’t make an application object oriented
Jul 16, 2013 @ 12:22:34

In this recent post Brandon Savage suggests that just using objects in your application doesn't mean that it's truly "object oriented development." There's other criteria that need to be met to really fit this description.

Lots of developers understand that object oriented code offers advantages over procedural programming. And so, they begin working on creating objects in their own projects, and eventually feel pretty good about what they’ve done. After all, if they’re using objects, their code must be object oriented, right? Well, not exactly.

He breaks it down into three main points that developers should consider when working with OOP in their apps: splitting responsibilities between classes, being polymorphic and using dependency injection. There's no code samples to back up the concepts here, but it's a decent list to think about. There's plenty of tutorials out there about SOLID development and dependency injection in PHP apps, so you might check some of those out to help with these concepts.

tagged: oop objectoriented development responsibility polymorphic dependencyinjection

Link: http://www.brandonsavage.net/using-objects-doesnt-make-an-application-object-oriented