Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Objects - Patterns and correct usage

I'm just wondering if someone can help me understand how to make the best use of objects in PHP.

My understanding of a PHP object is that is should represent an entity, providing methods to get and alter the properties of that entity. For example an object entitled Post would hold all the properties of a single post, which could be accessed and modified as appropriate.

What causes me some confusion is that libraries like CodeIgniter don't use objects in this manor. They treat classes more like wrappers for a group of functions. So a 'Posts' class in CodeIgniter would not hold properties of one post, it would provide functions for fetching, editing and deleting posts.

So what happens if I want to get every post out of a database and put it into a Post object? My understanding of it is I would in fact need two classes 'Posts' and 'Post', one that defines the Post object and one that handles fetching the Posts from the database and putting them into Post objects.

Do these two types of class have a name ('Proper' objects / Collections of functions)? And is it common to have two classes working together like this or have I completely misunderstood how to use objects?

Instead of having a Post object would it make more sense to have a method in my Posts class called getSinglePost($id) that just returned an array?

Hopefully that question makes sense, looking forwards to getting some feedback.

like image 886
jd182 Avatar asked Oct 07 '12 11:10

jd182


1 Answers

For an introduction, see What is a class in PHP?

For the answer, I'll just address your questions in particular. Search for the terms in bold to learn more about their meaning.

My understanding of a PHP object is that is should represent an entity, providing methods to get and alter the properties of that entity.

Entities are just one possible use for objects. But there is also Value Objects, Service Objects, Data Access Objects, etc. - when you go the OO route, everything will be an object with a certain responsibility.

What causes me some confusion is that libraries like CodeIgniter don't use objects in this manor.

Yes, Code Igniter is not really embracing OOP. They are using much more of a class-based-programming approach, which is more like programming procedural with classes and few sprinkles of OOP.

They treat classes more like wrappers for a group of functions. So a 'Posts' class in CodeIgniter would not hold properties of one post, it would provide functions for fetching, editing and deleting posts.

That is fine though. A posts class could be Repository, e.g. an in-memory collection of Post Entities that has the added responsibility to retrieve and persist those in the background. I'd be cautious with Design Patterns and Code Igniter though since they are known to use their own interpretation of patterns (for instance their Active Record is more like a Query Object).

So what happens if I want to get every post out of a database and put it into a Post object?

Lots of options here. A common approach would be to use a Data Mapper, but you could also use PDO and fetch the data rows directly into Post objects, etc.

My understanding of it is I would in fact need two classes 'Posts' and 'Post', one that defines the Post object and one that handles fetching the Posts from the database and putting them into Post objects.

That would be the aforementioned Repository or Data Mapper approach. You usually combine these with a Table Data Gateway. However, an alternative could also be to not have a Posts class and use an Active Record pattern, which represents a row in the database as an object with business and persistence logic attached to it.

Do these two types of class have a name ('Proper' objects / Collections of functions)? And is it common to have two classes working together like this or have I completely misunderstood how to use objects?

Yes, they work together. OOP is all about objects collaborating.

Instead of having a Post object would it make more sense to have a method in my Posts class called getSinglePost($id) that just returned an array?

That would be a Table Data Gateway returning Record Sets. It's fine when you don't have lots of business logic and can spare the Domain Model, like in CRUD applications

like image 82
Gordon Avatar answered Oct 25 '22 06:10

Gordon