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?
Pleas stop passing around objects by reference. It is not necessary since PHP5.x, instead object by default are passed around using handlers.
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().