PHP Developers Network

Proper Use of Objects
Page 1 of 1

Author:  Mike.Eller [ Fri Dec 05, 2014 2:48 pm ]
Post subject:  Proper Use of Objects

What would be the best practice for using objects?

I am designing a site for a baseball team.
There is a DB on the backend.
So, should I be creating objects for say "players" in order to load data from the database?
It seems unnecessary. I can just pull the data from the database and populate the page.

So would it make more sense to create objects based on how the page is laid out. For example, create a "roster" object that would hold the list of players pulled from the DB.

I know I can create objects for everything. But is it needed and is it the best way to do things.

I am of the mindset to use objects based on the page....i.e. rosters, announcements, stats section, etc versus using objects for what is going to populate those areas....i.e. players, games, at bat stats, etc...

Anyone have a push in the right direction?



Author:  Christopher [ Fri Dec 05, 2014 3:19 pm ]
Post subject:  Re: Proper Use of Objects

Well, you are looking for guidance in the most important and difficult part of software design. And there are a number of good practices that you will want to use. On the most general level, you want to define what is often called the Domain Model for you application (also referred to as the Application, Logic or Business Logic layer). The Domain Model contains many of the things you mention above: players, rosters, games, etc. The design of these objects is in many ways the artform of software design. Get it right and everything else slides into place.

As for your "mindset", it looks like you have mixed stuff together. Things like a stats section would display data from a player object, so that sounds like part of the other major part of an application which is the Presentation. The Presentation is where data from the Domain Model is displayed. Something like at bat stats might be accessed through the player object, even though they are probably in a separate table from the player record itself. Similarly, a team object might provide stats but for the team as a whole rather than a player. Things like: players, rosters, games, announcements, etc. sound like objects to me.

And obviously you don't "create objects for everything." At some point you are working with strings, numbers or arrays in a structured programming way. But objects should provide the major structure to you application.

MVC - if you application is using the MVC architecture then the Domain Model would be all of your Model classes, the Presentation would be your View and Controller classes. And the main rule to follow is to not have code in the Domain Model that is dependent on code in the Presentation. This follows the general N-Tier practice which support maintainability. Below both of these would be the Datasource which in PHP is a database connection object, wrapper around some other datasource like the session or filesystem, or some other data feed.

Author:  Mike.Eller [ Fri Dec 05, 2014 4:15 pm ]
Post subject:  Re: Proper Use of Objects

That makes sense.
But when am I actually using the objects? For example, the player object.
I have a form where I add players to my roster. So that would make sense to use the player object when doing that.
However, lets say the page loads that displays the roster. The page calls the code to get the list of players out of the DB and display them.
I do not see much use for the player object at this point.
Or, am I missing the whole thing? Should the player objects be created and stay in memory?
But at this point, everything is static.....

Author:  Christopher [ Fri Dec 05, 2014 5:51 pm ]
Post subject:  Re: Proper Use of Objects

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group