oop - Correct class design for a web project in PHP -
oop - Correct class design for a web project in PHP -
we're in process of rewriting our code in oop , i'm trying design general framework future projects (mostly comply dry , maintain code slim , consistent).
for illustration think nice have base of operations class databaseobject
, source properties database row. (among other things) utilize __call()
, property_exists()
automate getters. way we'd don't have potentially write 100+ getters every column in database row (or update class code every alter table).
other classes customer
, car
or article
extend databaseobject
(if they're sourced database) , inherit these methods, don't have re-create database specific code every class separately (which more , more inconsistent on time).
is correct...ish utilize of inheritance in web project, or bad design? colleague argues misuse of inheritance , client sourced csv file need separate class definition, valid point.
with interfaces we'd still have re-create code every class , traits (aside requiring php 5.4) not work __call()
illustration above (i think).
so there improve approach databaseobject
concept?
edit: explain reason downvotes me, can maybe delete , resubmit question in proper way?
you on right way mapping database table object. actually, people before invented wheel. there several orms in php can work without needs of reinventing wheel - e.g. doctrine, propel...
however, problems might encounter in current schema states of objects. entities - car
or article
have hard time keeping state if logic around in abstraction. you'd need properties mapped table columns in order maintain object changed, changes not commited. , uppon e.g. save()
method called, commit changes.
also, if need additional logic around this, let's say, need validate name of author of article, need setauthor()
method physically written anywhere in order alter things on it.
the orms have mentioned resolved these problems code generation tool, examines db schema , creates classes physical getters , setters in entity. propel, example, creates class table name, getters , setters in , logic there, creates class extends in public directory, when instantiate article
instantiate empty class extends articlebase
. articlebase
have setauthor()
method performs alter upon author e.g. update/insert. when need perform additional logic in setauthor()
override in article
, perform logic , phone call parent::setarticle()
.
this, also, resolves problem autocompletion. modern developers world made me rely much on auto complete. cannot , not remember public api of each module using, nor whole database lying under application. maybe don't know if articles
table has user_id
column author, or author_id
. thus, expect writing $article->
suggest me methods able use. scheme __call()
result lack of autocomplete suggestions, because no methods generation, nor annotations.
php oop inheritance
Comments
Post a Comment