Reputation: 592
i have this code. It works, but is it possible to do this better ? With less code ?
public function __construct($type ,$brand, $model, $year, $mileage, $height, $width, $depth, $ps, $color, $price, $value, $seats)
{
if (is_int($type)) {
$this->_type = $type;
} else {
throw new Exception('Wrong Value in Type. Expected integer');
}
}
...
I create an object like this
try {
$firstCar = new Car(1, 'Audi', 'A3', 2011, 94512, 2, 2, 2, 105, 'Black', 1000, 1920, 5);
} catch ( Exception $error) {
die($error->getMessage());
}
I repeat the "IF-ELSE" Statement for every variable in 2 Classes. Its so much code.
Some suggestions out here?
Upvotes: 1
Views: 137
Reputation: 116
It's not a lot of code, just messy.
Probably the problem is too many things in one place, group "types" of properties in values object, then each of these value object will handle their "validation"
example:
or something similar, you should know how they relate
would result in:
function __construct(CarType $model, Size $size, Price $price, $seats)
{
// no need to validade CarType did that
$this->model = $model;
....
}
To help in this object creation you could use a factory or a builder
Upvotes: 0
Reputation: 1439
For checking the values to be integer, you can use filter_var in callback way or you can pass array as arguments and use filter_var_array function in below way, below is my example code:
$firstCar = new Car(1, 'Audi', 'A3', 2011, 94512, 2, 2, 2, 105, 'Black', 1000, 1920, 5);
public function __construct($type ,$brand, $model, $year, $mileage, $height, $width, $depth, $ps, $color, $price, $value, $seats) {
$arrInput = func_get_args();
// this is enough to check only all arguments are integer or not by comparing the counts by input and filter array
$arrFilter = array_filter(filter_var_array($arrInput,FILTER_VALIDATE_INT));
if (count($arrFilter) < count($arrInput)) { // means there should be atleast one non-integer value
// to get the exact values which are exaclty not interger
$arrResult = array_diff($arrInput, $arrFilter);
throw new('your error'); // or return $arrResult; // this return Audi, A3 and Black
}
}
You can catch error using try/catch by throwing error from constructor or get the result values which are not integer by returning $arrResult from constructor, as per your need.
Upvotes: 0
Reputation: 592
I have the Problem solved differently.
$this->setType($type);
protected function setType($worth)
{
if (is_int($worth)) {
$this->_type = $worth;
} else {
throw new Exception('Wrong Value in Type. Expected valid integer');
}
}
This for each argument i passed to the Class.
For those who are interested in that. Not the best Solution, but it shortens the child classes.
Upvotes: 0
Reputation: 13816
Assuming, each field must be an int
:
...
private function checkArgument($arg) {
if (is_int($type)) {
$this->_type = $type;
} else {
throw new Exception('Wrong Value in Type. Expected integer');
}
}
...
public function __construct($type ,$brand, $model, $year, $mileage, ...) {
checkArgument($type);
checkArgument($brand);
...
}
You could also use func_get_args()
, to get all argument in an array, and loop on them.
Upvotes: 1