Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Items Class in PHP, abstract class products, example

Posted on 2010-11-14
10
Medium Priority
?
1,039 Views
Last Modified: 2012-05-10
Hello,
I am building an inventory, where items will be some sort of products.

I have a dilemma in how to write this class in php5, I want to write an abstract class item.

Now, which is a better approach, to write a separate DataManager interface to create and manage items (ex. add/update them into the DB) or to create methods inside the items class?

If you have an example of how to approach this situation feel free to share.

Thanks,
Eugen
0
Comment
Question by:navaru
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34131316
Is this a classroom assignment?  Can you show us an example of the code you've tried already?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34131335
0
 

Author Comment

by:navaru
ID: 34132106
It's not a classroom assignment, I'm new to OOP and I see that almost everyone has his own method of creating abstract classes. I quite don't fully understand some aspects, how should I approach this to be maintainable over time if I want to extend the items properties and category.  
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:marklorenz
ID: 34145358
This should answer your questions:

"PHP 5 introduces abstract classes and methods."
http://php.net/manual/en/language.oop5.abstract.php

 As this describes, an abstract class cannot be instantiated, but instead it is subclassed with concrete classes that can be.  I can answer any OO questions, but I am not a PHP expert so can't help with the details.  Glad to help further if I can...Mark
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34145545
Just going back to this in the original post, "I want to write an abstract class item."

Please tell us: why do you want to do that?  Can you show us an example of the code you've tried already?

Thank you, ~Ray
0
 

Author Comment

by:navaru
ID: 34154523
In the application I want to build, an item has properties and belongs to a category. Some of the Items are products to be sold, some are office inventory (furniture, appliances and so on).

I wanted to know how to best approach this so I can modify it over time and to have an easy readability over the code.

I thought it this way, every object is an item, but with different properties and purposes, but every item has a basic set of properties that I need in all of the situations.

abstract class item
{
  // basic data model
  // basic methods
}

interface dataManager
{
  // CRUD
}

products extends items implements dataManager
{
  // extra details
}

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34154607
That seems like a reasonable design.  Can you please post the code you have started with?
0
 
LVL 12

Accepted Solution

by:
marklorenz earned 1500 total points
ID: 34157399
How about this design?  (see attached diagram)

In this, each Item has its attributes and optionally is part of a Category and/or is Sellable.  You can expand upon this by subclassing Item as needed.  Moving between non-retail items and retail (for sale) only requires the Item to answer isForSale() return true or false. Inventory Item class diagram
0
 
LVL 12

Expert Comment

by:marklorenz
ID: 34157408
btw, you may want to put this question in the OMG UML zone.
0
 

Author Closing Comment

by:navaru
ID: 34741487
I was a good Idea, thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question