Answer the question
In order to leave comments, you need to log in
Is an object needed in this example?
class Book
{
public $name;
public $price;
public function __construct(string $name, int $price){
$this->name = $name;
$this->price = $price;
}
}
$a = new Book('Название', 1000);
$stmt = $db->prepare("INSERT INTO `books` (`name`, `price`) VALUE (?, ?)");
$stmt->execute(array($a->name, $a->price));
Answer the question
In order to leave comments, you need to log in
What's the alternative?
Working with arrays and hoping that everyone who works with your code will remember all the fields of all objects and never make a mistake in their name?
In this example, this seems redundant, but such code will be easier to maintain and extend in the future.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question