samedi 21 février 2015

Constructor injection and method usage

I have a couple of functions I'm rewriting to OOP classes. I'm still very new to OOP and is also using this to learn the basic concepts of OOP.


This is my first class written with the following in mind




  • Unit testing/isolation testing




  • A class should only do one thing




  • Re-usability




This class takes 4 user set variables and tests it against parameters set in the URL, and then returns 4 conditionals, one for every variable set


Here is the class (I have removed some methods and properties which does the same thing as the isAuthorReferrer() method)



namespace PG\Single\Post\Navigation;

/**
* Test set values against the super global given. Returns conditional properties
* which is boolean values. true is returned on success and false on failure
*
* @param $superGlobalVar Super global to test the values against
* @param (string) $authorReferrer
* @param (string) $dateReferrer
* @param (string) $searchReferrer
* @param (string) $taxReferrer
*
*/
class RequestReferrerHandler implements RequestReferrerHandlerInterface
{
/**
* @var (array) $superGlobalVar
*/
protected $superGlobalVar;

/**
* @var (bool) $isAuthorReferrer
*/
protected $isAuthorReferrer;

//OTHER PROPERTIES

/**
* Public constructor method
*
* @param $SuperGlobalVar Super global to get data from
*/
public function __construct($superGlobalVar = null, $authorReferrer= null, $dateReferrer = null, $searchReferrer = null, $taxReferrer = null )
{
/**
* Properties
*/
$this->superGlobalVar = $superGlobalVar;
$this->authorReferrer = $authorReferrer;
$this->dateReferrer = $dateReferrer;
$this->searchReferrer = $searchReferrer;
$this->taxReferrer = $taxReferrer;

/**
* Conditional methods, all returns boolean values
*/
$this->isAuthorReferrer();
//etc
}

/**
* Test $authorReferrer against $superGlobalVar
*
* @return (bool) true on success or false on failure
*/
public function isAuthorReferrer()
{
if ($this->authorReferrer && isset($this->superGlobalVar[$this->authorReferrer])) {
$isAuthorReferrer = true;
} else {
$isAuthorReferrer = false;
}
return $this->isAuthorReferrer = $isAuthorReferrer;
}

//OTHER METHODS, SAME AS isAuthorReferrer() METHOD

/**
* Returns an array of super global variables
* @return (array) $this->getRequest
*/
public function getSuperGlobalVar()
{
return $this->superGlobalVar;
}

}


I have read some posts that said you should make use of constructor injection, so have written my class around that.


I do have a few concerns here, one being instantiating my methods in the constructor


MY TEST



$a = new PG\Single\Post\Navigation\RequestReferrerHandler($_GET, 'aq', 'dq', 'sq', 'tq');
?><pre><?php var_dump($a); ?></pre><?php


With the code above, I get the following output



object(PG\Single\Post\Navigation\RequestReferrerHandler)#496 (9) {
["superGlobalVar":protected]=>
array(1) {
["tq"]=>
string(10) "category 1"
}
["isAuthorReferrer":protected]=>
bool(false)
["isDateReferrer":protected]=>
bool(false)
["isSearchReferrer":protected]=>
bool(false)
["isTaxReferrer":protected]=>
bool(true)
["authorReferrer"]=>
string(2) "aq"
["dateReferrer"]=>
string(2) "dq"
["searchReferrer"]=>
string(2) "sq"
["taxReferrer"]=>
string(2) "tq"
}


If I remove my methods from the constructor, my four conditional properties returns NULL, but I do get the correct boolean values if I call the methods directly like



?><pre><?php var_dump($a->isAuthorReferrer()); ?></pre><?php


The results from the var_dump() is:


if aq is set



bool(true)


if aq is not set



bool(false)


MY QUESTION


Am I using the methods correctly in the constructor and is my class even correctly set up


Aucun commentaire:

Enregistrer un commentaire