Jeff
Jeff

Reputation: 1774

PHP custom object casting

I have a custom class object in PHP named product:

final class product
{   
    public $id;
    public $Name;
    public $ProductType;
    public $Category;
    public $Description;
    public $ProductCode;
}

When passing an object of this class to my Data Access Layer I need to cast the object passed into a type of the product class so I can speak to the properties within that function. Since type casting in PHP works only with basic types what is the best solution to cast that passed object?

final class productDAL
{
    public function GetItem($id)
    {
        $mySqlConnection = mysql_connect('localhost', 'username', 'password');
        if (!$mySqlConnection) { trigger_error('Cannot connect to MySql Server!'); return; }
        mysql_select_db('databaseName');
        $rs = mysql_query("SELECT * FROM tblproduct WHERE ID='$id';");
        $returnObject = mysql_fetch_object($rs, 'product');
        return $returnObject;
    }

    public function SaveItem($objectToSave, $newProduct = false)
    {
        $productObject = new product();
        $productObject = $objectToSave;

        echo($objectToSave->Name);
        $objectToSave->ID;

    }
}

Right now I am creating a new object cast as a type of product and then setting it equal to the object passed to the function. Is there a better way of accomplishing this task? Am I going about the wrong way?

EDITED FOR CLARITY - ADD FULL PRODCUTDAL CLASS

Upvotes: 5

Views: 20473

Answers (4)

Matěj Zábský
Matěj Zábský

Reputation: 17272

Type casts in PHP are done like this:

$converted = (type) $from;

Note, that this won't work if the object types are not compatible (if for example $form happens to be a string or object of mismatching type).

But usual solution (called Active Record pattern, present for example in Zend Framework) is to have a base class for a database item called Row. Individual items (for example the class product from your sample) then inherit from this class.

Typical ZF scenario:

$table = new Product_Table();
$product = $table->find($productId); // load the product with $productId from DB
$product->someProperty = $newPropertyValue;
$product->Save(); // UPDATE the database

Which is IMO much better than your solution.

EDIT:

You can't cast between two unrelated objects, it is not possible.

If you want to use the DAL like this, skip the "product" object and go for simple associative array. You can enumerate over its members with foreach, unlike object's properties (you could use reflection, but that's overkill).

My recommendation: Go for the Active Record pattern (it is easy to implement with magic methods). It will save you a lot of trouble.

Upvotes: 1

jeroen
jeroen

Reputation: 91734

I´m not sure what you are trying to achieve, but if $objectToSave is already of class product:

  • You can simply call $objectToSave->SaveItem() (assuming SaveItem() is part of the product class) and access it´s properties in the function like $this->Name, etc.;
  • In your code $productObject and $objectToSave will hold a reference to the same object.

Upvotes: 1

giraff
giraff

Reputation: 4711

Currently, you are creating a new Product, then discarding it immediately (as its reference is replaced by $objectToSave.) You will need to copy its properties one by one, I regret.

foreach (get_object_vars($objectToSave) as $key => $value)
{
    $product->$key = $value;
}

(If the properties of $objectToSave are private, you will need to a expose a method to_array() that calls get_object_vars($this).)

Upvotes: 0

Sjoerd
Sjoerd

Reputation: 75588

You don't need to cast the object, you can just use it as if it was a product.

$name = $objectToSave->Name;

Upvotes: 9

Related Questions