Why should I abstract from the data layer?

The principles of OOP were difficult for me because for some reason I could never apply them to web development. As I developed more and more projects, I began to understand how some parts of my code can use certain design patterns to make them easier to read, reuse, and maintain, so I started using it more and more.

The only thing I still can not understand is why I should abstract my data layer. Basically, if I need to print a list of elements stored in my database into a browser, I do something like:

$sql = 'SELECT * FROM table WHERE type = "type1"';'
$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result))
{
    echo '<li>'.$row['name'].'</li>';
}

I read all of these How-Tos or articles preaching about the greatness of PDO, but I don’t understand why. I don’t seem to save LoCs, and I don’t see how it will be used more than once, because all the functions that I call above just seem to be encapsulated in the class, but they do the same. The only advantage that I see in PDO is prepared statements.

I am not saying that data abstraction is bad, I ask these questions because I am trying to develop my current classes correctly and they need to connect to the database, so I decided that I would do it the right way. Maybe I'm just reading bad articles on this topic :)

I would really appreciate any advice, links or concrete real-life examples on this subject!

+3
source share
5 answers

.

, , - , mysql , API- php mysql, .

, .

, , sql , .

, , , .

, (, ), mysql . . - .

+2

.

. , . , SQL, . N . , , , sql.

.

, . . , SQL Sqlite MySQL. .

. , .

, . , , .

+3

php, , .

, - , - / , .

, CHANGE

, . , , , , , . pictures.php?&uid=xxx.

SQL mysql. , / ? 5-10 ? , , , , .

Facebook. , , , SQL ??

:

  • .
  • .
  • . (, , )

,

+3

, / .

"" .

1) . , , . , , . , . - 2.

2) , . , /, . , . , , , .

, , , , , , . , .

3) . . , , . , - , . , .

, , ? , . ; , , , , , , - .

4) . , , . , , ? , SelectAllCustomers . , , .

, . , , , , . /-, , , .

+1

, , : , .

, , .

CMS , , .

. , , _id.

.

, , :

// Home page
$articles = $db->getTable('Article')->join('Author a')
    ->addSelect('a.name AS author_name');
$first_article_tags = $articles[0]->getRelated('Tag');

// Author profile
$articles = $db->getTable('Article')->join('Author a')
    ->addSelect('a.name AS author_name')->where('a.id = ?', $_GET['id']);

// Tag search results
$articles = $db->getTable('Article')->join('Author a')
    ->addSelect('a.name AS author_name')
    ->join('Tag')->where('Tag.slug = ?', $_GET['slug']);

, :

// Home page
$articles = Author::getArticles();
$first_article_tags = $articles[0]->getRelated('Tag');

// Author profile
$articles = Author::getArticles()->where('a.id = ?', $_GET['id']);

// Tag search results
$articles = Author::getArticles()
    ->join('Tag')->where('Tag.slug = ?', $_GET['slug']);

There are other good reasons for abstraction more or less, with its pluses and minuses. But, in my opinion, for most web projects, this is the main one: P

0
source

All Articles