Reputation: 43785
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 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.
edit: to further clarify what I'm struggling with the most is just the "bones" of an application. There are so many ways to accomplish the same thing. I understand how to package things up in classes and inheritance, I just don't know how those things should be used. Here's an example of what I'm looking for. How should this look?
$diC = new diC(); //dependency injection container
$router = $diC->get('router');
$api = $diC->get('api'); //this class probably makes no sense
if (!$router->collection) { //api/collection - no data requested
$data = $api->defaultResponse(); //what ought to take care of this?
}
else {
$route = $diC->get("collection_{$router->collection}"); //instantiate the class that will handle the collection request
$method = ($router->item) ? "{$router->verb}_item" : $router->verb; //$route->GET or $route->GET_item (for api/collection/item)
$data = $route->$method(); //get data for the request
}
$output = $api->format($data, $router->type); //again...what should be in charge of formatting the data?
echo $output;
In addition to having NO clue about how the above code really ought to look, I also want to have an array of errors ($errors = []
) that almost any part of the application can add to. I'm not sure of a clean way to make that available and get the data returned. An error class that everything is dependent on seems bad.
Upvotes: 4
Views: 401
Reputation: 7586
You should not be looking to understand "PHP OOP" as this is the same as any other OOP language just with different syntax, advantages and disadvantages. You should be researching the OOP principles and how to utilise OOP effectively.
I recommend buying some OOP books and even try and learn a strongly typed language such as Java. This will stand you in good steed to use the techniques/practices learned in other languages such as PHP.
I think the problem is you feel like you are doing something wrong (by looking at your example). You should take a look at a PHP framework that will handle the database and requests for you.
These are just generic examples that outline the basic structure, take a look at this tutorial using the Laravel framework.
http://net.tutsplus.com/tutorials/php/laravel-4-a-start-at-a-restful-api/
Upvotes: 2