Home arrow PHP arrow Page 4 - Storing Class Properties of Persistent Objects in MySQL Tables

Building a database-driven persistent class - PHP

Welcome to the fifth part of a six-part series that shows you how to build persistent objects in PHP 5. In this part of the series, I'll show you how to develop a persistent class that can save its properties to a MySQL table.

TABLE OF CONTENTS:
  1. Storing Class Properties of Persistent Objects in MySQL Tables
  2. Review: saving class properties to a text file
  3. Creating a MySQL abstraction class
  4. Building a database-driven persistent class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
September 29, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To demonstrate how to build a persistent class that will store its properties in a MySQL table, I’m going to redefine the “User” class utilized in this article and in previous ones, which hopefully will make this development process much easier to understand for you.

Having clarified that point, here’s the new definition of the aforementioned class, which now looks like this:

// define 'User' class (stores object properties on a database table)

class User

{

private $data = array();

private $id = 1;

 private $db = NULL;

 

// constructor

public function __construct(MySQL $db, $id = NULL)

{

$this->db = $db;

if ($id !== NULL)

{

$this->id = $id;

}

$this->db->query('SELECT * FROM users WHERE id=' . $this->id);

$this->data = $this->db->fetch();

}

 

// set undeclared property

public function __set($property, $value)

{

if ($property !== 'name' and $property !== 'email')

{

return;

}

$this->data[$property] = $value;

}

 

// get undeclared property

public function __get($property)

{

if (isset($this->data[$property]) === TRUE)

{

return $this->data[$property];

}

}

 

// save class properties to MySQL table

 public function __destruct()

{

$this->db->query("UPDATE users SET name = '$this->name', email = '$this->email' WHERE id = $this->id");

}

}

As I explained earlier, the skeleton of this “User” class remains nearly the same as its previous versions, except for its constructor and destructor methods, which are the ones that actually do the dirty work. The first method is responsible for retrieving the properties of the class from a specified MySQL table, while the second one is tasked with saving them back.

In this specific example, I used the term “retrieving” on purpose because the constructor uses an injected instance of the MySQL class defined previously to retrieve the properties from a fictional “users” MySQL table according to the supplied ID. On the other hand, the destructor simply updates the corresponding table record with new data, in case the properties have been updated as well.

Of course, the best way to understand how the above persistent class functions is by way of a concrete example, but this will be created in the last part of the series. Anyway, I guess that at this point you have a clear idea of how to spawn a persistent object by using the two classes defined before. So, if you feel this will be a no-brainer process, then go for it!

Final thoughts

In this penultimate part of the series, I went through the development of a persistent class that had the ability to save its properties to a MySQL table. Naturally, if you’re like me, it’s probable that you want to see an example that shows how to use this class. However, this example will be created in the final article, so now that you’ve been warned, you don’t have any excuses to miss it!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: