Question: PHP 7: Update the constructor in the Dog class to also allow the passing of the dog_gender value (Male or Female). Update the lab.php program

PHP 7:

Update the constructor in the Dog class to also allow the passing of the dog_gender value (Male or Female). Update the lab.php program to pass the gender through the constructor. Also update lab.php to explode the $lab string into five parts (one more for the dog_gender). Then evaluate the results (TRUE or FALSE) returned from the gender_error to display Gender update successful or Gender update not successful.

Dog.php:

class Dog

{

// ----------------------------------------- Properties -----------------------------------------

private $dog_weight = 0;

private $dog_breed = "no breed";

private $dog_color = "no color";

private $dog_name = "no name";

private $error_message = "??";

// ---------------------------------- Constructor ----------------------------------------------

function __construct($value1, $value2, $value3, $value4)

{

$name_error = $this->set_dog_name($value1) == TRUE ? 'TRUE,' : 'FALSE,';

$breed_error = $this->set_dog_breed($value2) == TRUE ? 'TRUE,' : 'FALSE,';

$color_error = $this->set_dog_color($value3) == TRUE ? 'TRUE,' : 'FALSE,';

$weight_error= $this->set_dog_weight($value4) == TRUE ? 'TRUE' : 'FALSE';

$this->error_message = $name_error . $breed_error . $color_error . $weight_error;

}

//------------------------------------toString--------------------------------------------------

public function __toString()

{

return $this->error_message;

}

// ---------------------------------- Set Methods ----------------------------------------------

function set_dog_name($value)

{

$error_message = TRUE;

(ctype_alpha($value) && strlen($value) <= 20) ? $this->dog_name = $value : $this->error_message = FALSE;

return $this->error_message;

}

function set_dog_weight($value)

{

$error_message = TRUE;

(ctype_digit($value) && ($value > 0 && $value <= 120)) ? $this->dog_weight = $value : $this->error_message = FALSE;

return $this->error_message;

}

function set_dog_breed($value)

{

$error_message = TRUE;

(ctype_alpha($value) && strlen($value) <= 35) ? $this->dog_breed = $value : $error_message = FALSE;

return $this->error_message;

}

function set_dog_color($value)

{

$error_message = TRUE;

(ctype_alpha($value) && strlen($value) <= 15) ? $this->dog_color = $value : $this->error_message = FALSE;

return $this->error_message;

}

// ----------------------------------------- Get Methods ------------------------------------------------------------

function get_dog_name()

{

return $this->dog_name;

}

function get_dog_weight()

{

return $this->dog_weight;

}

function get_dog_breed()

{

return $this->dog_breed;

}

function get_dog_color()

{

return $this->dog_color;

}

function get_properties()

{

return "$this->dog_weight,$this->dog_breed,$this->dog_color.";

}

}

?>

Lab.php:

Require_once("e312dog.php");

$lab = new Dog('Fred','Lab','Yellow','100');

list($name_error, $breed_error, $color_error, $weight_error) = explode(',', $lab);

print $name_error == 'TRUE' ? 'Name update successful
' : 'Name update not successful
';

print $breed_error == 'TRUE' ? 'Breed update successful
' : 'Breed update not successful
';

print $color_error == 'TRUE' ? 'Color update successful
' : 'Color update not successful
';

print $weight_error == 'TRUE' ? 'Weight update successful
' : 'Weight update not successful
';

// ------------------------------Set Properties--------------------------

$dog_error_message = $lab->set_dog_name('Sally');

print $dog_error_message == TRUE ? 'Name update successful
' : 'Name update not successful
';

$dog_error_message = $lab->set_dog_weight('5');

print $dog_error_message == TRUE ? 'Weight update successful
' : 'Weight update not successful
';

$dog_error_message = $lab->set_dog_breed('Labrador');

print $dog_error_message == TRUE ? 'Breed update successful
' : 'Breed update not successful
';

$dog_error_message = $lab->set_dog_color('Brown');

print $dog_error_message == TRUE ? 'Color update successful
' : 'Color update not successful
';

// ------------------------------Get Properties--------------------------

print $lab->get_dog_name() . "
";

print $lab->get_dog_weight() . "
";

print $lab->get_dog_breed() . "
";

print $lab->get_dog_color() . "
";

$dog_properties = $lab->get_properties();

list($dog_weight, $dog_breed, $dog_color) = explode(',', $dog_properties);

print "Dog weight is $dog_weight. Dog breed is $dog_breed. Dog color is $dog_color.";

?>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!