Archive for the ‘Classes & Examples’ category

PHP HTTPRequest Class

January 10th, 2008

Note: This is is a quick example of a very basic HTTP Request Class in PHP. This would be a nice, practical introduction to classes for someone who has been looking for a place to start.

Problem: By default, request data in PHP is handled by using Super Globals ($_POST, $_GET, $_COOKIES). Globals are bad for several reasons (outside the scope of this example google here). In an effort to remedy this, we need a way to encapsulate the request data into an object. By doing this, we are centralising access to request data through the one channel – the HTTP Request object.

Class Name:HTTPRequest

Responsibilities:

  • Store “request data” in GPC order.
  • Store additional “request data” outside of the super globals referenced by a key.
  • Read stored “request data” by referencing a key.
  • Internally clean request data by handling magic_quotes_gpc and then adding slashes.
  • Allow access to data stored in GET, POST and COOKIE super globals.

Collaborators: Any – the request object is used by any other class or function that requires access to the request data.

» Read more: PHP HTTPRequest Class