1

I'm attempting to build a REST based web service that provides a currency conversion function.

Please note that I'm a beginner to OO PHP so this is all new and fairly confusing to me.

I want the service to receive URL's encoded as:

http://www.mysite.com/conv/?amnt=7.15&from=GBP&to=USD

and I want it to return the results in the browser as an XML message. Error handing obviously needs to be catered for as well.

I've read numerous online tutorials and understand that I'll be using GET for what I'm trying to achieve. What confuses me the most is how to structure my PHP files (in OO format) and how to actually output these results on the default index page.

tctc91
  • 1,343
  • 2
  • 21
  • 41

2 Answers2

1

What's for you need OOP here?

Only if it's a selfeducational project. I think you should use something like MVC pattern, there is a lot of documentation out there, simply (here) translate that model into OOP php classes, and than put it into some way like this:

  • lib/
    • core/ - the core classes such as controller, request, response, e.t.c.
    • hepler/ - where the hepler's functions files are allocated
  • config/ - configuration for your DB connection, and other
  • model/ - you OOP model
  • view/ - the html templates
  • web/ - DOCUMENT_ROOT, where css, js, image, and your's controller access point in allocated
  • log/ - log dir, if you need one
  • cache/ although, if you need that.

Files, for easy explore should be named as *.class.php, if there any inheritance, it would be *.base.class.php, or *.module.class.php

Basic rules are:

  • One class per file
  • If you are using namespaces (recommended), then you shoul allocate files in the same way as namesapces, to easy autoload (spl_register_autoload())
  • Separate lib for each functionality

In your index file there would be only 3-4 lines, where you construct you project root class (controller), and then inside your controller you handle request, then going to the module, which you requested, and then to the view, where your system outputs the result.

OOP is greatly covered in MVC frameworks such as Symfony, you should look there.

devdRew
  • 4,393
  • 3
  • 24
  • 33
  • Thanks. That helps a lot in terms of structure. I'm also quite confused by what classes are need for what I'm trying to achieve. I guess the first class is to detect if it's a GET request and if the message is encoded correctly, run the currency convert function. How do I lay that out and how would I integrate error management into that? Sorry, I'm very new to this so forgive how basic my questions are. – tctc91 Dec 30 '11 at 16:18
  • Basic classes must be Controller (controller.class.php), Request (request.class.php), Response (response.class.php). In the entry point you should create $controller = new Controller('index'); And in __construct() method you need to initialize ONE instance of Request class. there would be a method, names is($type), that would give answer for question what type of request is this. [howto](http://stackoverflow.com/questions/359047/php-detecting-request-type-get-post-put-or-delete) and if all is ok, put $_GET var in local class storage to work with. Also you need method get($what), to get params. – devdRew Jan 02 '12 at 07:59
0

Being the Author of the Restler (a PHP based RESTful API Server) framework, I would like to suggest that you try Restler. It takes a different and simpler approach and exactly fits your bill. It exposes public methods of your class as an API.

BMI Example and other live examples should get you started in no time

Arul Kumaran
  • 983
  • 7
  • 23