Saturday, 28 September 2013

Understanding php class structure from a javascript OOP background (how to adapt code workflow)

Understanding php class structure from a javascript OOP background (how to
adapt code workflow)

I have been attempting to write a simple RESTful api in php. Part of my
goal in doing this is to get a good grasp on how to approach php OOP
projects. In javascript, I have no issue with encapsulation, inheritance,
etc. I generally start out with a single object (maybe even just as a
namespace) that I will nest everything in, i.e.var appName = {}. A simpler
object may just be a group of methods...then are a few typical needs like
an init function to set things up and/or execute/run function in which I
can detail a flow. I like to use a few general functions there that
summarize everything that will happen, then detail them, then detail
those, etc, so it is always easy to see how the result is produced, and
you can investigate the details further as needed. I also keep everything
independent so that each function doesn't have to worry about how it gets
its dependencies, etc. So, I hope that gives you an idea of where I'm
coming from.. I am utterly lost in the world of oop php.
I went ahead and wrote a simple api in js just to demonstrate what I'm
trying to do. It could be better, but this is just a quick example.
http://jsbin.com/AmIfApO/3/edit I'll include that code at the end of my
post. I have attempted for weeks to achieve this functionality in php, and
have failed, failed, failed. I can make it work, but it's just awful code
and I am determined to write it using best practices. For the sake of not
appearing lazy, here's the latest php rewrite I tried....ended in disaster
like always http://pastebin.com/cKw9YeVz.
With js, I eventually saw the right example/ got the right advice and the
idea became pretty clear. I think that seeing even just the basic design
flow of how this would be done in php would probably help me tie in
everything I've learned. Something just hasn't clicked. If you can give me
an example of what classes I should have here or how to divide things up,
I would be so appreciative.
var request1 = {
collection: 'articles',
item: null,
verb: 'GET',
format: 'json'
};
var request2 = {
collection: 'articles',
item: null,
verb: 'DELETE',
format: 'json'
};
var api = {
request : (function() {
//parse url into collection, item, format, verb
return request1; //change to request2 for error test
}()),
output : {
data: {},
errors: []
},
respond : function() {
var handler = this.handler(this.request);
this.output.data = this.format(handler());
console.log(this.output);
},
error : function(msg) {
this.output.errors.push(msg);
},
handler : function(request) {
var handler;
if (request.collection) {
if (request.item) {
handler = this.handlers[request.collection].item;
}
else {
handler = this.handlers[request.collection].collection;
}
}
else {
handler = this.handlers.def;
}
return handler[request.verb];
},
format : function(output) {
return output;
},
handlers : {
def: {
collection : {
GET : function() {
return 'generic test data!';
},
POST : function() {
},
PUT : function() {
},
DELETE : function() {
api.error("Collection can't be deleted");
}
},
item : {
GET : function() {
},
POST : function() {
},
PUT : function() {
},
DELETE : function() {
}
}
}
}
};
api.handlers.articles = Object.create(api.handlers.def);
api.handlers.articles.collection.GET = function() {
return 'specific test data!';
};
api.respond();

No comments:

Post a Comment