Jump to content

Critique and help improve my URI class


acefirefighter

Recommended Posts

I am fairly comfortable with procedural PHP but when it comes to classes and OO I am learning.

 

I have written a small class to get all of the information from the URL. I am sure I have added my own style and broken about a million rules. I have already found places to improve this but I thought I would bounce it off everyone here before I started to make changes and started expanding it. I am also aware that there are classes that I can download that do this much better but I am trying to better understand how they work so I think this is a good start.

 

Can you just look it over and point out things that I have done wrong and give me some general pointers on how to improve it.

class uri extends mainframe{

private $path = null;
private $pathParse = array();
private $component = null;
private $view = null;
private $host = null;
private $dirDepth = null;
public  $queryString = array();

function __construct() {
	$this->getHost();
	$this->getPath();
	$this->getView();
	$this->getQueryString();
       	
}
/*
 * Check to see if we are in the base folder
 */
function dirDepth($base) {
	$this->dirDepth = config::DDEPTH + $base;

	return $this->dirDepth;
}
/*
 * return the host address
 */
function getHost() {
	$this->host = $_SERVER['HTTP_HOST'];

	return $this->host;
}
/*
 * return the path information
 */
function getPath() {
	$this->path = $_SERVER['REQUEST_URI'];

	return $this->path;
}
/*
 * returns the query string in an array
 * 
 * I am sure this isn't the right way to do this
 * but it is working.
 */
function getQueryString() {
	$this->getPath();
	preg_match('/\?(.*)/', $this->path, $queryString);
	if ($queryString == true) {
		$queryPairs = array();
		$queryString = (isset($queryString['1']) ? $queryString['1'] : null);
		$queryPairs = explode('&', $queryString);
		$queryStrings = array();
		$pairs = array();
		foreach ($queryPairs as $queryPairs) {
			preg_match('/(.*)=(.*)/', $queryPairs, $pairs);
			array_push($queryStrings, $pairs);
		}
		$key = array();
		$value = array();
		foreach ($queryStrings as $queryStrings) {
			array_push($value, (isset($queryStrings['2']) ? $queryStrings['2'] : null));
			array_push($key, (isset($queryStrings['1']) ? $queryStrings['1'] : null));

		}
		$this->queryString = array_combine($key, $value);

		return $this->queryString;
	}else{
		unset($this->queryString);
	}
}
/*
 * returns the path in an array and removes the query string
 */
function pathParse() {
	self::getPath();
	$this->pathParse = explode('/', $this->path);
	$endCheck = preg_replace('/\?(.*)/','', array_pop($this->pathParse));
	array_push($this->pathParse, $endCheck);
	$this->pathParse = array_filter($this->pathParse);
	if(!empty($this->pathParse)) {
		return $this->pathParse;
	}else{
		unset($this->pathParse);
	}
}
/*
 * returns the first part of the path
 */
function getComponent() {
	self::pathParse();
	self::dirDepth('1');
	if(!empty($this->pathParse[$this->dirDepth])) {
		$this->component = $this->pathParse[$this->dirDepth];

		return $this->component;
	}else{
		unset($this->component);
	}
}
/*
 * returns the second part of the path
 */
function getView() {

	self::pathParse();
	self::dirDepth('2');
	if(!empty($this->pathParse[$this->dirDepth])) {
		$this->view = $this->pathParse[$this->dirDepth];

		return $this->view;
	}else{
		unset($this->view);
	}
}
/*
 * Ummmmm need some help here for sure.
 */
function __destruct() {
}
}

$uri = new uri();

 

Thank you in advance for your help!

 

Link to comment
Share on other sites

1.  Is there any reason to call those methods from the constructor rather than letting them be called on demand?

2.  It seems like your "get" methods do a lot of work - I would expect a get method just to return the value with minimal work.  You could do this by remembering if you've calculated the result yet.

3.  What if I want to use your class to parse another URI, rather than the one in $_SERVER?

 

There's no need for a destructor, as your class has no external resources.  All the variables will be garbage collected by PHP.

Link to comment
Share on other sites

1.  Is there any reason to call those methods from the constructor rather than letting them be called on demand?

2.  It seems like your "get" methods do a lot of work - I would expect a get method just to return the value with minimal work.  You could do this by remembering if you've calculated the result yet.

3.  What if I want to use your class to parse another URI, rather than the one in $_SERVER?

 

There's no need for a destructor, as your class has no external resources.  All the variables will be garbage collected by PHP.

 

1. No there really isn't it was more just me testing things out.

2. I think I see what your saying here. All of my "get" methods should really do is just return the value from the server and not, for example, separate the query string and place in in an associative array? This should happen elsewhere? Thanks for the tip that is a good one.

3. I thought about this but I wasn't too sure how to implement it. I had found a php function called "parse_url" and was thinking that I could create a method that would take input from a variable and if there were none it would default back to $_server and return a parsed url.

 

I couldn't figure out how to use the destructor in this case. Just because you have tool doesn't mean you have to use it. (PS. Don't tell my wife I said that.)

 

Hey, thanks for your help. That is exactly the sort of thing I was looking for. I think I am the only PHPer in my town and this forum has been an awesome resource.

 

Anyone else want to rip that apart for me I would apprciate it.

 

Thank you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.