PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Oct 15, 2019 10:22 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Proper Use of Objects
PostPosted: Fri Dec 05, 2014 2:48 pm 
Offline
Forum Newbie

Joined: Fri Dec 05, 2014 2:40 pm
Posts: 2
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


Top
 Profile  
 
PostPosted: Fri Dec 05, 2014 3:19 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
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)


Top
 Profile  
 
PostPosted: Fri Dec 05, 2014 4:15 pm 
Offline
Forum Newbie

Joined: Fri Dec 05, 2014 2:40 pm
Posts: 2
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.....


Top
 Profile  
 
PostPosted: Fri Dec 05, 2014 5:51 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group