25.1k views
2 votes
Create a PHP class called "Product" with the following properties and methods:

Properties:

name string)

price (float)

• quantity (int)

Methods:

construct): A constructor method that sets the initial values of the name, price, and quantity properties.

getName(): A method that returns the value of the name property.

getPrice): A method that returns the value of the price property. getQuantity: A method that returns the value of the quantity property.

setNameSname): A method that sets the value of the name property.

setPrice Sprice): A method that sets the value of the price property.

setQuantity(Squantity): A method that sets the value of the quantity property.

getTotalPrice(): A method that calculates and returns the total price of the product based on the price and quantity properties.

Instructions:

Create a new instance of the "Product" class and set the name to "thigh snack", the price to 7.50, and the quantity to 3.

Call the "getName()" method on the instance and print the result.

Call the "getPrice()" method on the instance and print the result.

Call the "getQuantity)" method on the instance and print the result.

Call the "getTotalPrice()" method on the instance and print the result.

Call the "setName()" method on the instance and set the name to "conch snack".

Call the "setPrice)" method on the instance and set the price to 10.95.

Call the "setQuantity)" method on the instance and set the quantity to 5.

Call the "getName()" method on the instance and print the result.

Call the "getPrice()" method on the instance and print the result.

Call the "getQuantity)" method on the instance and print the result.

Call the "getTotalPrice(J" method on the instance and print the result.

1 Answer

2 votes

Final answer:

To create the PHP class 'Product' with the given properties and methods, and perform the instructions provided.

Step-by-step explanation:

To create the PHP class 'Product' with the given properties and methods, you can use the following code:

<?php
class Product {
private $name;
private $price;
private $quantity;

public function __construct($name, $price, $quantity) {
$this->name = $name;
$this->price = $price;
$this->quantity = $quantity;
}

public function getName() {
return $this->name;
}

public function getPrice() {
return $this->price;
}

public function getQuantity() {
return $this->quantity;
}

public function setName($name) {
$this->name = $name;
}

public function setPrice($price) {
$this->price = $price;
}

public function setQuantity($quantity) {
$this->quantity = $quantity;
}

public function getTotalPrice() {
return $this->price * $this->quantity;
}
}


To perform the given instructions:

<?php
$product = new Product('thigh snack', 7.50, 3);
echo $product->getName();
echo $product->getPrice();
echo $product->getQuantity();
echo $product->getTotalPrice();
$product->setName('conch snack');
$product->setPrice(10.95);
$product->setQuantity(5);
echo $product->getName();
echo $product->getPrice();
echo $product->getQuantity();
echo $product->getTotalPrice();

User Loan
by
8.2k points