Insert, update database record using OOP
Hi!
I am learning object oriented programming and would like a few pointers from the wiser. I would like to make changes to make changes to the table in the database (insert, update, delete record). I am wondering if that is possible to achieve via a class? To have class student and inside defined functions selectStudent, insertStudent and updateStudent(id). The benefit would obviously be to have only one place to look up for the changes in the table structure and not the whole code.
I have seen it done in some examples, but only the result => John.setAge = 23, John.updateStudent(2).
Can you give me some pointers, examples, tutorials, ...? Thank you!
Re: Insert, update database record using OOP
You can read the nest article http://www.tonymarston.net/php-mysql...seobjects.html and you'll good information about your question.
Re: Insert, update database record using OOP
Quote:
Originally Posted by
omci
Hi!
I am learning object oriented programming and would like a few pointers from the wiser. I would like to make changes to make changes to the table in the database (insert, update, delete record). I am wondering if that is possible to achieve via a class? To have class student and inside defined functions selectStudent, insertStudent and updateStudent(id). The benefit would obviously be to have only one place to look up for the changes in the table structure and not the whole code.
I have seen it done in some examples, but only the result => John.setAge = 23, John.updateStudent(2).
Can you give me some pointers, examples, tutorials, ...? Thank you!
There is a number of design patterns you could use. For example the Strategy pattern or the Visitor pattern. Both of them let you separate the algorithm from the data. That is you implement "insert", "update" and other algorithms not in the Student class but rather in a separate class (or rather set of classes) that accept your data object (i.e. Student instance) as an argument. However whether any of those approaches fit your use-case, it depends on that use-case.
Re: Insert, update database record using OOP
Thank you both, just the information I was looking for but could not find it on my own!