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

  • SEARCH
  • Home
  • 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 8722443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:25:15+00:00 2026-06-13T07:25:15+00:00

I created a item class (item.class.php) and itemDAO class (itemDAO.class.php) and I have another

  • 0

I created a item class (item.class.php) and itemDAO class (itemDAO.class.php) and I have another php file (test.php). I want to inset a data to database using those classes from test.php class. Also I have a database class (db.class.php). Here I’m stuck with by getting the object’s (item) values to the function in itemDAO class.

Here is my item.class.php

<?php

class item {

    private $id;
    private $name;
    private $description;


    public function item($_id,$_name,$_description){

        $this->id=$_id;
        $this->name=$_name;
        $this->description=$_description;

    }

    public function setId( $id )
    {
        $this->id = $id;
    }

    public function setName( $name )
    {
        $this->name = $name;
    }

    public function setDescription( $description )
    {
        $this->description = $description;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getDescription()
    {
        return $this->description;
    }       
}
?>

This is my itemDAO.class.php

<?php
include("db.class.php");
include("item.class.php");

class itemDAO {

    public function __construct() {
        $d=new db();
    }

    public function AddItem(&$item) { //I am stuck here 

        $result = $db->query("INSERT INTO item VALUES('$i->') ");//Im stuck here 
    }
}
?>

How can I get the item class object to get values from the object and pass to the sql query.
I’m poor in php and know java well.
In java we can just call like this

public boolean addItem(Item I) {

    String query = "insert into Item(Item_Id, Item_Name, Item_Category, Measuring_Unit, Last_Price_Rate)  values ('" + I.getItemID() + "','" + I.getItemName() + "','" + I.getItemCategory() + "','" + I.getMeasuringUnit() + "','" + I.getLastPriceRate() + "')";
    System.out.println(query);
}

this is my test.php

<?php
include("item.class.php");
include("db.class.php");
include("itemDAO.class.php");
$id="3";
$name="aaa";
$descriptio="hi";
$item= new item($id,$name,$descriptio);

$dao=new itemDAO();
$dao->AddItem($item->getId(),$item->getName(),$item->getDescription());
?>

my database class

<?php

class db {

    private $link;
   // private $host="localhost", $username="root", $password="123", $database="item";

    public function __construct(){
        $this->host        = "localhost";
        $this->username    = "root";
        $this->password    = "123";
        $this->database    = "item";

        $this->link = mysql_connect($this->host, $this->username, $this->password)
            OR die("There was a problem connecting to the database.");

        mysql_select_db($this->database, $this->link)
            OR die("There was a problem selecting the database.");

        return true;
    }

    public function query($query) {
        $result = mysql_query($query);
        if (!$result) die('Invalid query: ' . mysql_error());
        return $result;
    }

    public function __destruct() {
        mysql_close($this->link)
            OR die("There was a problem disconnecting from the database.");
    }
}
?>

How can I do this same thing with PHP with Object Orientation?

  • 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-13T07:25:16+00:00Added an answer on June 13, 2026 at 7:25 am

    Pleas stop passing around objects by reference. It is not necessary since PHP5.x, instead object by default are passed around using handlers.

    class itemDAO{
    
        protected $db;
    
        public function __construct( $db ){
            $this->db= $db;
        }
    
        public function AddItem( $item ){ 
    
            $query = 'INSERT INTO Item(Item_Id, Item_Name, Item_Category)  
                      VALUE ( :id, :name, :category )';
            $statement = $this->db->prepare( $query );
            $statement->bindParam( ':id', $item->getId(), PDO::PARAM_INT );
            $statement->bindParam( ':name', $item->getName() );
            $statement->bindParam( ':category', $item->getCategory() );
    
            return $statement->execute(); // returns false if query fails.
        }
    
    }
    

    If you know the basics of PHP have previous experience with Java, then I would recommend you to read “PHP in Action”. That will cover OOP in PHP in Java-friendly manner.

    Also I suspect, that you might benefit from reading this article. It would explain you how to use PDO API ( assuming that this is what you were using already).

    And you need to learn how to utilize spl_autoload_register().

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

Sidebar

Related Questions

I have created a class, it store the data from mySQL database into class.
When a new item is created using Tastypie, I want to be able to
I have created a php class which is pretty expensive and needs to process
i created a class derived from CheckedListBox so i can change item height as
I'm trying to create some Menu Item with Image & Label, using barry's class
I have a simple unordered list with list items as menu item i created
I created a method to unmarshall my xml (item.xml) file .But if there are
I created a customized CursorAdapter and want to select a list item, in order
I have an abstract parent class Item , from which different types of items
I'm trying to make a class that takes some unspecified data from a database

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.