1

My question is how should i be inserting php class objects into the database? If i have

Cat.php

class Cat{ private $name; public function setName($name){ $this->name = $name } public function database(){ ....$this->name; //Insert stuff into db } } 

Somefile.php

if($_POST['catname']){ //This way? $obj1 = new Cat(); $obj1->setName($_POST['catname']); $obj1->database(); //Or this way //Insert Cat directly into the db without creating an object of Cat } 

And i want to insert this cat into the database. Would it be best to create a function within cat that gets $this->name or before i create the object should i just insert it into the database?

2
  • What type of database? I'd say create a Serializable interface, then implement writeObject and readObject methods on any class that is serializable. Commented May 31, 2013 at 14:30
  • i am using mysql currently Commented May 31, 2013 at 14:37

6 Answers 6

2

The easiest way of doing it would be to serialize your class to string.

$string = serialize($cat); 

http://php.net/manual/en/language.oop5.serialization.php

But this way you will create lots of overhead in your database.

The best way to do it would be to have the relevant fields in your table and just save the corresponding values. You can easily create a function which reads / saves your class to a table. This would make your data much more portable and exchangeable.

Sign up to request clarification or add additional context in comments.

2 Comments

This would be the "easiest" approach I think.
Not very appropriate, but definitely the easiest :P
2

You can use serialize, and if you have a good autoloader, you will not have to worry about the class not existing when you unserialize.

Also note the 2 magic functions that you can use:

__sleep() - custom code to use when serializing an object

__wakeup() - custom code when unserializing an object

PS:

Replace:

$obj1->setName = $_POST['catname']; 

with

$obj1->setName($_POST['catname']); 

References:

1 Comment

ok i will look into serialize more. Good catch. i made changes
1

Ideally, the object itself should deal with saving itself to the database. This way, your logic doesn't need to know what cat is, or where it is saving itself. The cat class should be self-contained, and this means if you want to change DB types or anything else in the future, you can make it in the class without affecting your logic.

3 Comments

One way I know of using functions inside a class without creating an instance of that object is to use the parent operator. Something like Cat::database(); Not sure if that's what your asking though
I am not sure this is the good way... and for two reasons: 1. the day you change the database you have to touch the model objects which is not good. 2. it goes against one principle of OOP : The Single Responsibility Principle... what do you think ?
@mlwacosmos Your suggestion of several layers is definitely a valid approach, but I was really trying to get OP to cleanly separate their logic from their data layer without complicating things by adding additional layers or design patterns. If OP only had a small handful of classes on a tiny project, I think my approach is valid
1

You should have a separate object that takes an instance of cat and puts the data into the database. You may want to have a method of getVitalData which provides all the data that should go into the database (you may eventually have more than just name).

Each class should have one responsibility

Comments

1

Normally there are several layers in your project and one of this layer is the DAO (Data Access Object). In this class you link your application to a database. this layer has to save your object into the database via a save method for example...

The day you wanna change the database you just rewrite this layer

To be really ideal, the object that is present in the DAO layer is different from the model object (CAT) : it is a persistent Object that looks like the table in the database

Comments

0

Take a look at serialize and unserialize, you can store the serialised string in your database if required - here's a quick example.

<?php class Cat { protected $name; public function getName() { return $this->name; } public function setName($name) { $this->name = $name; return $this; } } $cat = new Cat; $cat->setName('Tiddles'); $string = serialize($cat); $cat = unserialize($string); var_dump($cat); /* object(Cat)#2 (1) { ["name":protected]=> string(7) "Tiddles" } */ 

Anthony.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.