I feel that PHP classes are really useless

Hello all,
I really like to code in OOP style, but I find it really useless when it comes to PHP for some reason.
I’ll give a simple example of what I tried to do, I got a page that is intended to show latest news, so I select the data from the DB and display it right away iterating on fetch_assoc();
Lately to make the code clear I wanted to try out to write the code in OOP, so each field will have its own getter/setter and update/display methods on the class for easier handling, but I just feel like it makes the code look even more messy, stuff like $news[‘title’] just turn to $news->getTitle() without any real point.

I think it will become usefull only if you can store the objects you create somewhere for future use instead of using SQL query all the time, like in a session variable, so my second question is, do you think its recommanded storing such data in session variables to avoid future queries for a certain user for like 20 minutes?

1 Like

I love PHP OOP specially now that I’m getting into Class Abstraction and Object Interfaces.

For example I have this as an interface:

<?php

/* The iCrud interface.
 * The interface identifies four methods:
 * - create()
 * - read()
 * - update()
 * - delete()
 */

interface iCrud {

  public function create($data);

  public function read();

  public function update($data);

  public function delete();
  
}

Here’s just a snippet of my LoginClass

// This method also takes an array of data:
  public function create($data) {
    self::__construct($data);
  }

// Function for returning information about the current object:
  public function read() {
    return array(
        'userId' => $this->_userId,
        'username' => $this->_username,
        'first_name' => $this->_first_name,
        'last_name' => $this->_last_name,
        'email' => $this->_email
    );
  }

// Function for updating the object:
  public function update($data) {
    $this->_username = $data['username'];
  }

// Functin for getting rid of the current object:
  public function delete() {
    $this->_userId = NULL;
    $this->_username = NULL;
    $this->_first_name = NULL;
    $this->_last_name = NULL;
    $this->_email = NULL;
  }

Then all I have to do for my PHP code is this

$database = new LoginClass();
$submit = filter_input(INPUT_POST, 'submit', FILTER_SANITIZE_SPECIAL_CHARS);
if (isset($submit) && $submit == 'Submit') {
  $data['username'] = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
  $data['password'] = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);
  $database->create($data); // Login User:  
}

$info = $database->read(); // Read in user's information from database table:

/* Logout User */
$logout = filter_input(INPUT_POST, 'logout', FILTER_SANITIZE_SPECIAL_CHARS);
if (isset($logout) && $logout == 'yes') {
  // Delete the record:
  $database->delete(); 
  // Delte the object:
  unset($database);
}

I’m also 99 percent positive I can get rid of the arrays by reading in the data as a class. For me OOP keeps me from getty sloppy with my code and the code is very portable from project to project. Just my .02 cents.

Well PHP array is very powerful compared to this feature in many other languages, for this reason it renders OOPHP somewhat less useful than other languages. However, I dont think your example $news->getTitle() is what we generally mean by OOP. Instead its just the very basic or surface of OOP, the true beauty of OOP comes when you apply more useful methods rather than getter/setter. If you think OOP exists primarily for getter/setter, even for a language like PHP, then you are serious mistaken.

In a procedural style, we tend to think of our program as a bunch of data with functions twiddling their bits. But in the OO style, we try to hide that data, to encapsulate it behind a conceptual abstraction.

Classes such as your News would probably be considered a value object, and admittedly they’re among the more boring kinds of objects, because they tend to be little more than state containers. But objects don’t just contain data, they contain functions, and sometimes you pass around an object for the behavior it provides more so than its data. And that’s where OO software architecture gets more interesting. There will be classes that do a task, classes that route tasks, classes that make other classes, and so on.

I have a similar view to @ulthane as I can do what I want in normal inline code or with a function. Possibly on larger sites it has more uses?

I tried using it and ended up writing a lot more code than I would have done without using classes. This has the additional effect that I have a problem understanding how it works as I can not see the point of it :confused:

I am told one of the reasons to use it is portability but functions are portable and again unless you are using the class for exactly the same thing you would need to change or expand it from one site to another anyway.

Well main reason I asked this question is to get any solutions for lowering the amount of database connections on each page load on the site.

My thought was to create objects from SQL when a user enter the site for the first time and store them somehere for a certain amount of time (like in sessions for 20 minutes), then whenever he reloads the page or go to a page that uses the same objects they’ll just get loaded from the session without the need for another SQL query, this idea is taken from my java application dev experience, where you load all data from SQL on app startup and then you got all objects available until application is closed without the need to use any SQL again.

So I would like to know if its smart doing it in such a way or there’s any oter ways to speed up page loads.

Sounds like caching is what you need.