Probably, one of the most illustrative ways to demonstrate how the class "fits" into an application is by coding an example. Therefore, here's a possible implementation of the class: require_once 'mysqlclass.php'; require_once 'dbigeneratorclass.php'; // instantiate a new MySQLConnector object $db=&new MySQLConnector(array // instantiate a new DBIGenerator object $g=&new DBIGenerator('users','User','DBICLASSES/'); // generate class file $g->generate(); // get an user object if(!$user=$g->getObject()){ die('Failed to create object'); } In the above example, we included the class files, instantiating a $db "MySQLConnector" object to provide the necessary database connectivity. Then, we instantiated a "DBIGenerator" object, passing to it the name of the database table to be associated with, in this case "users"; the name of the DBI class to be created, "User"; and lastly, the path where the class file will be located, that is "DBICLASSES/". For this example, the "users" table contains the following fields: "id","firstname", "lastname" and "email", which implies that the class tied to it should represent these fields as properties, as well as having the corresponding setters and getters. After executing the snippet, a class file named "User.php" is created in the "DBICLASSES/" directory, and a "User" class has been dynamically generated, with the following definition: <?php class User{var $id='';var $firstname='';var Don't get confused about the code! The class has been simply created with no new lines on it. Since the PHP interpreter doesn't care about this, the file size is significantly reduced. However, to have a more readable version of the class, I show the same code, this time including the new lines: <?php class User{ var $id=''; var $firstname=''; var $lastname=''; var $email=''; function User(){} function setid($id){ $this->id=$id; } function getid(){ return $this->id; } function setfirstname($firstname){ $this->firstname=$firstname; } function getfirstname(){ return $this->firstname; } function setlastname($lastname){ $this->lastname=$lastname; } function getlastname(){ return $this->lastname; } function setemail($email){ $this->email=$email; } function getemail(){ return $this->email; } function load(){ $r=mysql_query("SELECT * FROM users WHERE id='$this->id'"); return mysql_fetch_array($r,MYSQL_ASSOC); } function submit(){ mysql_query("INSERT INTO users SET firstname='$this- $this->id=mysql_insert_id(); } function update(){ mysql_query("UPDATE users SET firstname='$this- } function delete(){ mysql_query("DELETE FROM users WHERE id='$this->id'"); } } ?> Wasn't that good? I'm sure you'll agreed. The above class is a logical representation of the "users" table. Having such a useful class, we're able to perform all of the single-row DML operations directly on the table. Let's consider the power of this approach when we combine these classes to access multiple tables. It's really worth giving a try. That's what we will do in the next article! Wrapping up A few considerations are needed here. Over this part of the series, I've coded a new "DBIGenerator" class that presents greater flexibility to work with any database table supplied as class parameter. Also, hopefully I've demonstrated how powerful this class can be for accessing data by using a single communication point. However, the real strength of this approach is clearly evidenced when working with many classes tied to multiple tables. That's the pending task for the next article, so you don't have any excuses to miss it. See you soon!
blog comments powered by Disqus |
|
|
|
|
|
|
|