Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 550
Jeff Avatar asked Feb 08 '11 15:02

Jeff


People also ask

How do you typecast in PHP?

The way by which PHP can assign a particular data type for any variable is called typecasting. The required type of variable is mentioned in the parenthesis before the variable. PHP does not support data type for variable declaration.

How create object explain with example in PHP?

Following is an example of how to create object using new operator. class Books { // Members of class Books } // Creating three objects of Books $physics = new Books; $maths = new Books; $chemistry = new Books; Member Functions: After creating our objects, we can call member functions related to that object.

What is type and type Conversion in PHP?

Datatype conversion is the process of changing the type of the variable from one datatype to another. PHP provides various datatypes and the type conversion can be performed in various methods. Since, PHP is a loosly typed language, it allows variable declaration without specifying its datatype.

What is a PHP object?

Definition and Usage In PHP, Object is a compound data type (along with arrays). Values of more than one types can be stored together in a single variable. Object is an instance of either a built-in or user defined class. In addition to properties, class defines functionality associated with data.


3 Answers

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

$name = $objectToSave->Name;
like image 179
Sjoerd Avatar answered Oct 22 '22 22:10

Sjoerd


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.
like image 37
jeroen Avatar answered Oct 23 '22 00:10

jeroen


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.

like image 1
Matěj Zábský Avatar answered Oct 22 '22 22:10

Matěj Zábský