Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9302087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:10:31+00:00 2026-06-18T23:10:31+00:00

I am relatively new to OOP approach to PHP, but just wanted something clarified:

  • 0

I am relatively new to OOP approach to PHP, but just wanted something clarified:

if(isset($_POST['insertProduct']) && !empty($_POST['productName'])){
    $newProduct = new product();
    $newProduct->productName = $_POST['productName'];
    $newProduct->servingSize = $_POST['servingSize'];
    $productID = $newProduct->insertProduct();   
}

So the above code is executed when a form is posted to the page to enter a new product into the DB.

$selectProduct = new product();
if($selectProduct->getProducts()){
    foreach($selectProduct->getProducts() as $product){ 
        echo '<option value="' .
            $product['productID'] . '">' . 
            $product['productName'] . '</option>';
    }
}

The above piece is on the same page, and obviously now there are two instances of the product class that exist (assuming the form has been posted to add a new product). Does this show a benefit of the OO approach? As in using two instances to access different methods? Or is it a bad way of doing what I want to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:10:32+00:00Added an answer on June 18, 2026 at 11:10 pm

    I think getProducts should be at least a static method, since it doesn’t have nothing to do with the instance of a product.
    A product class should represent only the single data of a product, therefore generic non-instances methods should be set to static or moved to another class.

    I’d suggest you to create a Factory class that will create the product instances for you:

    $array = FactoryProducts::getProducts();
    

    But you surely need to move that method outside the product class.
    For the rest, everything seems fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Relatively new to C#; hopefully I'm just overlooking something simple. I have a form
I'm relatively new using OOP in PHP. It's helped immensely in the organization and
Relatively new to PHP here, but here's where I'm at: I have a simple
I'm relatively new to OOP, so wanted to clear a few things, I have
I'm relatively new to OOP in PHP, and I'm not sure if what I'm
I'm relatively new to PHP OOP and cant seem to be able to call
Im relatively new to PHP but have realized it is a powerfull tool. So
I am relatively new to OOP so I am not sure about the terminology.
Relatively new to JavaScript, so wondering what is the best approach to achive the
Relatively new to WinForms but I'm working on a small business app. Anyway, where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.