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

6

u/equilni Jun 17 '23

The first is more preferable as the insert method is relying on the Book object with enforced types and defined properties versus an unenforced array.

[
    'title'   => $title, # Is title a string, integer?
    'summary' => $summary, # What is summary here? 
    'author'  => $author # Is this a string of first name/last name, pen name or an object??
]

versus:

class Book { 
    public function __construct(
        public readonly string $title, # PHP 8.1 readonly
        public readonly ?string $summary,
        public readonly Author $author
    ) {
    }
}

class BookMapper {
    public function select(): Book {}
    public function insert(Book $book) {}
    public function update(Book $book){}
    public function delete(Book $book) {}
}

1

u/silentheaven83 Jun 17 '23 edited Jun 17 '23

Thank you. What about multiple related objects?

For eg. I have a book_title table that store multilanguage book titles (bookId, lang, title): (1, "en", Odyssey), (1, "it", "Odissea")

Should I do this or perhaps create a BookService that does it all in a createBook() method?

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

foreach ($_POST['titles'] as $lang => $title) {
    $BookTitle = new BookTitle();
    $BookTitle->bookId = $Book->id;
    $BookTitle->lang = $lang;
    $BookTitle->title = $title;

    $BookTitleMapper->insert($BookTitle);
}

1

u/equilni Jun 18 '23 edited Jun 18 '23

For eg. I have a book_title table that store multilanguage book titles

What the difference then between Book::title and this BootTitle::title? It seems confusing to say the least.

create a BookService that does it all in a createBook() method?

A service would work for this.

1

u/silentheaven83 Jun 18 '23

I'm sorry, I have used the Book "thing" as an example and I forgot the obvious conflict of those two kind of titles.

I meant if I had to store multilanguage titles in an another table instead of just one Book::title.