M
M
Max Ba2020-04-13 13:45:11
PHP
Max Ba, 2020-04-13 13:45:11

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));


Not quite clear. Made Class Book. Filled it up. And what should I do with it next? Transfer to the class an intermediary between Him and the database and save inside?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Gordinskiy, 2020-04-13
@phpcoder81

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 question

Ask a Question

731 491 924 answers to any question