r/PHPhelp • • Jun 17 '23

Understanding Array vs Object

Hello everybody,

sorry if it's a dumb question but I need your help.

Between these two solutions for saving a Book after a form submit:

// book_submit.php
$BookMapper = new BookMapper(
    $Database,
    $Log
);
$Book = new Book();
$Book->title = $_POST['title'];
$Book->summary = $_POST['summary'];
$BookMapper->insert($Book);

and

// book_submit.php
$BookMapper = new BookMapper(
    $Database,
    $Log
);
$BookMapper->insert(
    array(
        "title" => $_POST['title'],
        "summary" => $_POST['summary']
    )
);

// Or even
$BookMapper->insert($_POST);

which one is considered the best and why?

The BookMapper class will return a Book object from its select() method, here I'm only talking about saving data.

Thank you

9 Upvotes

13 comments sorted by

View all comments

Show parent comments

1

u/silentheaven83 Jun 17 '23

I completely got your point!

Thank you for the explanation.

1

u/whoisthis238 Jun 17 '23

No probs!

1

u/silentheaven83 Jun 17 '23

One last question if I can, should I validate the POST data in the book_submit.php file, or in the insert() method, or in a createBook() method of a BookService class that validates and call the BookMapper->insert() method?

Thank you!

1

u/whoisthis238 Jun 17 '23

Ideally i think you should have a separate Validator service. I don't think you should do it in mapper class. Then again it's kind of subjective, and hard to say without seeing the whole code.

From what I understand you are not using a framework. One of the advantages of using framework is that it will provide a consistent way to validate data.