Building Object-Oriented Database Interfaces in PHP: Working with Multiple Data Access Objects - Code generator in a nutshell: one class that generates another one (Page 3 of 5 ) Once we've called the "generate()" method, one data access object $user has been programmatically created. But let's go one step further: if the object is available to be used, there must be a class from which the object was properly instantiated. That's the main task of this generator class, as you might guess. One DB Interface class has been generated "behind the scenes," providing a bridge between the database table and the application logic. As you've seen before, this is the class that I obtain as a result, after executing the above code: 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- >firstname',lastname='$this->lastname',email='$this->email'"); $this->id=mysql_insert_id(); } function update(){ mysql_query("UPDATE users SET firstname='$this- >firstname',lastname='$this->lastname',email='$this->email' WHERE id='$this->id'"); } function delete(){ mysql_query("DELETE FROM users WHERE id='$this->id'"); } } Definitely, the "DBIGenerator" class does its thing building the corresponding database interface class for a specified database table. In this case, I'm showing a more readable version of the code generated, since in its obfuscated version, it doesn't contain any new lines. However, this is not a relevant issue for the PHP interpreter. Well, happily we've seen how a simple code generator works, because we have a class that is capable of generating one or more classes, according to the particular application requirements. Do you realize the actual power of this approach? I guess you do. However, let's implement some examples to see how a database interface can be put to work for us. | | Discuss Building Object-Oriented Database Interfaces in PHP: Working with Multiple Data Access Objects | | | | | | | Part three of the tutorial explains the advantages of working with multiple database... | | | | | | Thanks again Alejandro, this series has helped me understand a concept that until... | | | | | | Hello Jeff,Glad to hear from you again. It's important to know that this series... | | | | | | It's such a good article. | | | | | | Thank you for the kind words about my article. It's very much appreciated.Best... | | | | | | Hi Alejandro, thanks a lot for these articles. Although a difficult subject (for... | | | | | | Hello Matthijs,I feel very pleased of knowing that this series has been helpful... | | | | | | Hi Alejandro I am by far no expert but have a fairly decent grasp of php and oop,... | | | | | | Hello John,Thanks a lot for your kind words. I'm very pleased to know that my... | | | | | | Hi Alejandro defently looking foward to them especially the caching one. If at some... | | | | | | Hello John,Thank you for the message. Good to hear that the upcoming PHP... | | | | | | Hello,This is a very interesting article, but I have a question. Lets say that... | | | | | | Hello,Thank you for the comments on my article. They're much appreciated.With... | | | | | | Okay, that makes sense. And in fact, it is what I did before you posted. Glad we... | | | | | | Hello again,Regarding your question on your current project, even when I don't... | | | | | | Hi Alejandro,Thank you for taking the time write an truly excellent article on... | | | | | | Hello ShamzZ,Thank you for your kind comments on my PHP article; I'm glad to... | | | | | | The DBIGenerator is awesome. I've been wanting to do that for a while but never took... | | | | | | Hi again Sig,As I posted before, thank you for introducing your comments here.... | | | | | | Really enjoyed the article. I'm starting to dive into OO in PHP and this helped... | | | | | | Hi Paul,Thank you for the kind comments on my PHP article. I indeed appreciate... | | | | | | This series of Alejandro helps me complete a full blown, database driven website for... | | | | | | Hi Roy,Thank you for the kind comments on my PHP article, and I'm glad to know... | | | | | | I have to say, quite simply a staggeringly powerful and intuitive approach to... | | | | | | Thanks for the compliments, as they’re greatly appreciated, indeed. It’s always nice... | | | | | | >>> Post your comment now! | | | | | |
|
 | |