Problem with using classes

I want to use some “Classes” in my website. I thing it’s a good idea to have a class for users and a class for posts.
But I don’t know how to do it. I don’t know what is the best way to do it. How do developers do it in a real, big website?
I have some problem with it. for example: I can use mysqli_fetch_object() for storing data in classes but how about queries like this:

$query = "SELECT SQL_CALC_FOUND_ROWS "
       . "questions.question_id, questions.user_id, "
       .   "posts.post_id, posts.question_id, posts.user_id, posts.message, posts.image, posts.is_question, posts.created_at, posts.is_answer, "
       .   "IFNULL(SUM(votes.vote),0) AS vote, users.username, users.reputation "
       . "FROM posts "
       . "INNER JOIN questions USING(question_id) "
       . "INNER JOIN users ON(posts.user_id=users.user_id) "
       . "LEFT JOIN votes USING(post_id) "
       . "WHERE posts.question_id={$question_id} "
       . "GROUP BY post_id "
       . "ORDER BY posts.is_question DESC, $order "
       . "LIMIT $start, 10";

Please help me. thanks.

On large real websites the developers use framework, either a popular third party framework or build their own framework. Most frameworks follow the architecture MVC pattern, and the part M means model, which is what you are referring to in your question. Most Models are designed to fit the database structure of corresponding tables, for instance theres a User Model for User database table, same for post. The model class properties will be table columns, such as username, password, email for User Model/Table. The database information is usually accessed and loaded into Model objects using some kind of data access logic, or with DAO(data access object) whose main responsibility is to load and map database info into model object properties. Models are very powerful, if you use ORM(object relational mapping) for your site you can even establish model relations. For instance, a post may have an author user, and the author user is mapped to its corresponding user model when needed. There are lots of interesting tricks, and if you havent ever heard of terms like MVC and ORM, you still have a long way to go.

Even a small project can have MVC - Here’s a small portion of an online calendar that I’m developing:

Records Class:

<?php

class Records {

  // The record attributes containing required and optional information.
  // The attributes must correspond to the database table columns:	

  private $id = NULL;
  private $client_id = NULL;
  private $name = NULL;
  private $appDate = NULL;
  private $appTime = NULL;
  private $comments = NULL;

  // Method returns the user ID:
  public function getId() {
    return $this->id;
  }

  // Method returns client ID:
  public function getClientId() {
    return $this->client_id;
  }

  // Method returns name:
  public function getName() {
    return $this->name;
  }

  // Method returns appDate:
  public function getAppDate() {
    return $this->appDate;
  }

  // Method returns appTime:
  public function getAppTime() {
    return $this->appTime;
  }

  // Method returns comments:
  public function getComments() {
    return $this->comments;
  }

}

and how I’m going to go about retrieving it:

class InputOutput extends Database{

  protected function outputRecords() {
    try { // Fetch the page
      $query = 'Select id, name, appDate, comments FROM schedule WHERE name=:name AND appDate=:appDate';
      $result = $pdo->prepare($query);
      $result->execute(array(':name' => $name, ':appDate' => $appDate));
      $result->rowCount();
      if ($result && $result->rowCount() > 0) { // Check that rows were returned:
        $result->setFetchMode(PDO::FETCH_CLASS, 'Records'); // Arm the class by fetching it:
        while ($record = $result->fetch()) {
          
        }
      } else {
        
      }
    } catch (Exception $e) { // Catch generic exceptions
      log:error($e->getMessage());
    }
  }

}

I’ve been slowly getting a better grip on OOP in PHP, what I have found out is write classes that actually preform a useful method (pun intended) and the a lot of classes can be used over and over for different projects. An Object-Oriented Programming will be come more intuitive and classes that are developed will be better define (written).

For instance here’s my main file for the calendar - calendar.php

<?php
$pageTitle = "Graphical Calendar";

require('lib/includes/utilities.inc.php');
/* This makes sures that the input is a number */
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
if (!$page) {
  $page = 0;
}

/* Set the Calendar Controls to the correct page */
$setMonth = new CalendarControls($page);
/* Grab the previous or next month */
$todaysDate = $setMonth->returnDate;

/* This is the actual calendar for you could subsitute */
/* $todaysDate with you own script in figuring out     */
/* today's date or what have you                       */
$month = new MyCalendar($todaysDate);

include('lib/includes/header.inc.php');
?>

<section class="container mainContent">
  <!-- OUTPUT CSS STYLED CALENDAR -->
  <?php echo $month->generateForm; ?>
  <!-- OUTPUT CSS STYLED CALENDAR BUTTONS -->
  <?php echo $setMonth->returnControls; ?>
</section>

<?php
include('lib/includes/footer.inc.php');

An here’s a very, very beta of the calendar in action - http://localhost/php_test/magicCuckoo/calendar.php

I have also notice that it has increased the performance of the calendar writing it in a more logical manner. A calendar is a smaller project, but it utilize MVC that bigger projects do.