Proper Use of Objects

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Mike.Eller
Forum Newbie
Posts: 2
Joined: Fri Dec 05, 2014 1:40 pm

Proper Use of Objects

Post by Mike.Eller »

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?

Regards,

Mike
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Proper Use of Objects

Post by Christopher »

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.
(#10850)
Mike.Eller
Forum Newbie
Posts: 2
Joined: Fri Dec 05, 2014 1:40 pm

Re: Proper Use of Objects

Post by Mike.Eller »

OK.
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.....
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Proper Use of Objects

Post by Christopher »

Mike.Eller wrote:OK.
That makes sense.
But when am I actually using the objects? For example, the player object.
So a Player object/class makes a lot of sense, but at some there are a couple well know styles for objects like this. These styles that have been found to work over and over again are call Patterns. We can certainly walk through the design these classes if you like. Once your preferences are clear is should be pretty easy.
Mike.Eller wrote: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.
I would think that a page that loads a roster would use a Team object. From that Team object you could get a roster. That roster could be an array of Player objects. Then to display your roster you might do something like this in your Presentation code:

Code: Select all

$roster = $team->getRoster();
foreach ($roster as $player) {
     echo "<li>#" . $player->getNumber() . " - " .  $player->getName() . "</li>\n";
}
Mike.Eller wrote:Or, am I missing the whole thing? Should the player objects be created and stay in memory?
But at this point, everything is static.....
Well, the interesting thing about PHP is that everything is created and then destroyed each request. Usual practice is to let the shutdown code destroy everything (doing it yourself can actually slow down your code in some cases). PHP has what is called a shared nothing architecture. That is a fantastic thing in some peoples' eyes and a horrible thing in others'. Since we are a PHP forum, let's assume fantastic.

Not sure what you mean by everything is static? Can you clarify that?
(#10850)
Post Reply