Skip to main content
added 42 characters in body
Source Link
mheinzerling
  • 2.7k
  • 15
  • 17

demo should be Demo and in new classes you might want to use namespaces instead of underscores in your class names. See PSR-0

As non of your $args is optional and there are only 5 I would recommend the use real method parameters instead of this array. Otherwise some validation and documentation of this array is missing.

Furthermore you could refactor your ifs to real guard conditions and skip the else branch. This makes the code more readable and you get rid of a indentation level. There are also no braces on one-line-ifs.

Edit Sample for guard conditions

class PDOConnection_Factory { private function __construct() {} public static function build($args) { if (!isset($args)) return NULL; $statement = ... try { $dbh = ... return $dbh; } catch (PDOException $e) { //as Cygal stated this is not very nice echo 'Error: ', $e->getMessage(), "\n"; return NULL; } } } 

Edit 2 Handle many and optimal method/constructor parameter

First the usage:

new PDOConnectionBuilder().name("test").user("guest").password("1234").create(); 

And now the details:

class PDOConnectionBuilder() { private type="mysql"; //defaults private name; private host="localhost"; private user; private password; public function create() { //guards again if (empty($name)) throw new PDOConnectionBuilderException("Name is not optional"); if (empty($user)) throw ... if (empty($password)) $passwordToUse=$user; //fallback to default else $passwordToUse=$password; //do the required stuff return $connection; } public function name ($name) { $this->name=$name; return $this; } //... } 

This is a really awesome pattern for building complex object in larger system and in tests. You could even reuse the builder anand call create twice. Or change only one property and call create again.

demo should be Demo and in new classes you might want to use namespaces instead of underscores in your class names. See PSR-0

As non of your $args is optional and there are only 5 I would recommend the use real method parameters instead of this array. Otherwise some validation and documentation of this array is missing.

Furthermore you could refactor your ifs to real guard conditions and skip the else branch. This makes the code more readable and you get rid of a indentation level. There are also no braces on one-line-ifs.

Edit Sample for guard conditions

class PDOConnection_Factory { private function __construct() {} public static function build($args) { if (!isset($args)) return NULL; $statement = ... try { $dbh = ... return $dbh; } catch (PDOException $e) { //as Cygal stated this is not very nice echo 'Error: ', $e->getMessage(), "\n"; return NULL; } } } 

Edit 2 Handle many and optimal method/constructor parameter

First the usage:

new PDOConnectionBuilder().name("test").user("guest").password("1234").create(); 

And now the details:

class PDOConnectionBuilder() { private type="mysql"; //defaults private name; private host="localhost"; private user; private password; public function create() { if (empty($name)) throw new PDOConnectionBuilderException("Name is not optional"); if (empty($user)) throw ... if (empty($password)) $passwordToUse=$user; //fallback to default else $passwordToUse=$password; //do the required stuff return $connection; } public function name ($name) { $this->name=$name; return $this; } //... } 

This is a really awesome pattern for building complex object in larger system. You could even reuse the builder an call create twice. Or change only one property and call create again.

demo should be Demo and in new classes you might want to use namespaces instead of underscores in your class names. See PSR-0

As non of your $args is optional and there are only 5 I would recommend the use real method parameters instead of this array. Otherwise some validation and documentation of this array is missing.

Furthermore you could refactor your ifs to real guard conditions and skip the else branch. This makes the code more readable and you get rid of a indentation level. There are also no braces on one-line-ifs.

Edit Sample for guard conditions

class PDOConnection_Factory { private function __construct() {} public static function build($args) { if (!isset($args)) return NULL; $statement = ... try { $dbh = ... return $dbh; } catch (PDOException $e) { //as Cygal stated this is not very nice echo 'Error: ', $e->getMessage(), "\n"; return NULL; } } } 

Edit 2 Handle many and optimal method/constructor parameter

First the usage:

new PDOConnectionBuilder().name("test").user("guest").password("1234").create(); 

And now the details:

class PDOConnectionBuilder() { private type="mysql"; //defaults private name; private host="localhost"; private user; private password; public function create() { //guards again if (empty($name)) throw new PDOConnectionBuilderException("Name is not optional"); if (empty($user)) throw ... if (empty($password)) $passwordToUse=$user; //fallback to default else $passwordToUse=$password; //do the required stuff return $connection; } public function name ($name) { $this->name=$name; return $this; } //... } 

This is a really awesome pattern for building complex object in larger system and in tests. You could even reuse the builder and call create twice. Or change only one property and call create again.

added 538 characters in body
Source Link
mheinzerling
  • 2.7k
  • 15
  • 17

demo should be Demo and in new classes you might want to use namespaces instead of underscores in your class names. See PSR-0

As non of your $args is optional and there are only 5 I would recommend the use real method parameters instead of this array. Otherwise some validation and documentation of this array is missing.

Furthermore you could refactor your ifs to real guard conditions and skip the else branch. This makes the code more readable and you get rid of a indentation level. There are also no braces on one-line-ifs.

Edit Sample for guard conditions

class PDOConnection_Factory { private function __construct() {} public static function build($args) { if (!isset($args)) return NULL; $statement = ... try { $dbh = ... return $dbh; } catch (PDOException $e) { //as Cygal stated this is not very nice echo 'Error: ', $e->getMessage(), "\n"; return NULL; } } } 

Edit 2 Handle many and optinaloptimal method/constructor parameter

Give meFirst the usage:

new PDOConnectionBuilder().name("test").user("guest").password("1234").create(); 

And now the details:

class PDOConnectionBuilder() { private type="mysql"; //defaults private name; private host="localhost"; private user; private password; public function create() { if (empty($name)) throw new PDOConnectionBuilderException("Name is not optional"); if (empty($user)) throw ... if (empty($password)) $passwordToUse=$user; //fallback to default else $passwordToUse=$password; //do the required stuff return $connection; } public function name ($name) { $this->name=$name; return $this; } //... } 

This is a secondreally awesome pattern for building complex object in larger system. You could even reuse the builder an call create twice. Or change only one property and call create again.

demo should be Demo and in new classes you might want to use namespaces instead of underscores in your class names. See PSR-0

As non of your $args is optional and there are only 5 I would recommend the use real method parameters instead of this array. Otherwise some validation and documentation of this array is missing.

Furthermore you could refactor your ifs to real guard conditions and skip the else branch. This makes the code more readable and you get rid of a indentation level. There are also no braces on one-line-ifs.

Edit Sample for guard conditions

class PDOConnection_Factory { private function __construct() {} public static function build($args) { if (!isset($args)) return NULL; $statement = ... try { $dbh = ... return $dbh; } catch (PDOException $e) { //as Cygal stated this is not very nice echo 'Error: ', $e->getMessage(), "\n"; return NULL; } } } 

Edit 2 Handle many and optinal method/constructor parameter

Give me a second...

demo should be Demo and in new classes you might want to use namespaces instead of underscores in your class names. See PSR-0

As non of your $args is optional and there are only 5 I would recommend the use real method parameters instead of this array. Otherwise some validation and documentation of this array is missing.

Furthermore you could refactor your ifs to real guard conditions and skip the else branch. This makes the code more readable and you get rid of a indentation level. There are also no braces on one-line-ifs.

Edit Sample for guard conditions

class PDOConnection_Factory { private function __construct() {} public static function build($args) { if (!isset($args)) return NULL; $statement = ... try { $dbh = ... return $dbh; } catch (PDOException $e) { //as Cygal stated this is not very nice echo 'Error: ', $e->getMessage(), "\n"; return NULL; } } } 

Edit 2 Handle many and optimal method/constructor parameter

First the usage:

new PDOConnectionBuilder().name("test").user("guest").password("1234").create(); 

And now the details:

class PDOConnectionBuilder() { private type="mysql"; //defaults private name; private host="localhost"; private user; private password; public function create() { if (empty($name)) throw new PDOConnectionBuilderException("Name is not optional"); if (empty($user)) throw ... if (empty($password)) $passwordToUse=$user; //fallback to default else $passwordToUse=$password; //do the required stuff return $connection; } public function name ($name) { $this->name=$name; return $this; } //... } 

This is a really awesome pattern for building complex object in larger system. You could even reuse the builder an call create twice. Or change only one property and call create again.

added 538 characters in body
Source Link
mheinzerling
  • 2.7k
  • 15
  • 17

demo should be Demo and in new classes you might want to use namespaces instead of underscores in your class names. See PSR-0

As non of your $args is optional and there are only 5 I would recommend the use real method parameters instead of this array. Otherwise some validation and documentation of this array is missing.

Furthermore you could refactor your ifs to real guard conditions and skip the else branch. This makes the code more readable and you get rid of a indentation level. There are also no braces on one-line-ifs.

Edit Sample for guard conditions

class PDOConnection_Factory { private function __construct() {} public static function build($args) { if (!isset($args)) return NULL; $statement = ... try { $dbh = ... return $dbh; } catch (PDOException $e) { //as Cygal stated this is not very nice echo 'Error: ', $e->getMessage(), "\n"; return NULL; } } } 

Edit 2 Handle many and optinal method/constructor parameter

Give me a second...

demo should be Demo and in new classes you might want to use namespaces instead of underscores in your class names. See PSR-0

As non of your $args is optional and there are only 5 I would recommend the use real method parameters instead of this array. Otherwise some validation and documentation of this array is missing.

Furthermore you could refactor your ifs to real guard conditions and skip the else branch. This makes the code more readable and you get rid of a indentation level. There are also no braces on one-line-ifs.

demo should be Demo and in new classes you might want to use namespaces instead of underscores in your class names. See PSR-0

As non of your $args is optional and there are only 5 I would recommend the use real method parameters instead of this array. Otherwise some validation and documentation of this array is missing.

Furthermore you could refactor your ifs to real guard conditions and skip the else branch. This makes the code more readable and you get rid of a indentation level. There are also no braces on one-line-ifs.

Edit Sample for guard conditions

class PDOConnection_Factory { private function __construct() {} public static function build($args) { if (!isset($args)) return NULL; $statement = ... try { $dbh = ... return $dbh; } catch (PDOException $e) { //as Cygal stated this is not very nice echo 'Error: ', $e->getMessage(), "\n"; return NULL; } } } 

Edit 2 Handle many and optinal method/constructor parameter

Give me a second...

Source Link
mheinzerling
  • 2.7k
  • 15
  • 17
Loading