Class Declaration

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Class Declaration

Post by printf »

I notice many classes have a lot of variable declaration, fifty or more. So I've been wondering about what approach to take. Should I declare all those variables as a single variable type or should I use an array container so the class has one variable for each thing that is truly unique. For example, say I have a http request object, then that object might have a (request, response) array container to hold all the items related to each unique container or should I just declare individual variable assignments...


Code: Select all

class http
{
	/*
	* flag, is document chunked (bool)
	*/

	private $iv_chunked = false;

	/*
	* flag, is request complete (bool)
	*/

	private $iv_complete = false;

	/*
	* flag, is document compressed (bool)
	*/

	private $iv_compressed = false;

	/*
	* document encoding type (string)
	*/

	private $iv_encoding = 'none';

	/* ... */
}


// declare primary containers

Code: Select all

class http
{
	/*
	* the http information response container (array)
	*/

	private $iv_response = array( 'encoding' => 'none', 'complete' => false, 'chunked' => false, 'compressed' => false );

	/* ... */
}


TIA

pif
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

You're about half right - grouping related properties is the way to go. The core problem however is that whether the class is doing too much? If the class is long, with lists of properties than it's a good sign it could be broken up. For example, a HTTP Client should be handling the task of making HTTP requests and handling redirects, etc. Then to handle the data this requires I would have at least two more objects - Request and Response. This likely gets a lot of properties in the class you mentioned isolated in separate objects (you mentioned arrays, so not too far off this path).

For example, a good use case could be:

Code: Select all

$request = new Http_Request();
$request->setMethod(Request::GET);
$request->setUri('http://blog.example.com/rss');
$request->setHeader('Accept', 'application/rss+xml'); // content negotiation if enabled by target

// use client as a singleton instance
$response = Http_Client::getInstance()->request($request);

// have received a Http_Response object
if(!$response->isSuccess())
{
    throw new Exception('Error occured or request could not complete. Received status code:' . $response->getStatusCode());
}

$rss = new SimpleXMLElement( $response->getBody() );

// do something with the RSS XML
If you follow the logic, the Response object contains all the results - response-headers, http codes and messages, the body of the response, etc. Similar thinking for the Request object - it's used to build a request separate from the client.

Bit of a ramble - but should offer some food for thought I hope :).
Post Reply