r/PHPhelp • • Apr 08 '25

Right way to PHP OOP Implementation

Hi, I'm working as a full stack php developer. My job mainly requires procedural style php code. So I'm quite well versed with it.

Now, I have been trying to learn php oop. But no matter how many videos or guides i see, its still confusing.

Main confusion comes from

  1. File organization
  • should each class be a seperate php file
  • should utility class ( sanitization, uppercase, lowercase etc) be all combined in one?
  • how to use one class in another class
  1. How to create class
  • what should constitute a class. Should user be a class defining creation / deletion / modification of users
  • what exactly should a constructor of class do ( practically)

I'm still trying to defer mvc architecture for now. In order to understand how the design and flow for oop program should be done

Any and all help with this is helpful. I understand the basics, but having difficulty with irl implementation. Please recommend any guide that helps with implementation rather than basics.

Thanks

9 Upvotes

27 comments sorted by

View all comments

Show parent comment

2

u/equilni Apr 10 '25 edited Apr 12 '25

Can you recommend any repos or open source projects i can look at

To start:

  • Structuring the application:

https://phptherightway.com/#common_directory_structure

https://github.com/php-pds/skeleton

https://www.nikolaposa.in.rs/blog/2017/01/16/on-structuring-php-projects/

I typically quote this:

You could mix PDS-Skeleton and Slim's config files. Which means, to start:

/project    
    /config 
        dependencies.php - DI/classes
        routes.php - routes 
        settings.php
    /public 
        index.php
    /resources 
        /templates 
    /src 
        the rest of your PHP application - See Structuring PHP Projects previously linked, for more
    composer.json - If you want to work with composer for autoloading

Begin pseudo code:

settings.php. This could be a simple returned array like the linked Slim example

return [
    'app'         => [
        'charset'     => 'utf-8',  // for HTML header and htmlspecialchars
        'language'    => 'en-US' // can be added to html <html lang="en-US"> or language folder/file
    ],

    'template'    => [
        'path' => 'path to your templates folder'
    ],
];

dependencies.php would house all your class instances and allow for a Dependency Injection Container like PHP-DI. This could look like:

$config = require __DIR__ . '/config/settings.php';

$pdo = new \PDO(
    $config['database']['dsn'],
    $config['database']['username'],
    $config['database']['password'],
    $config['database']['options']
);

$classThatNeedsPDO = new classThatNeedsPDO($pdo);
$otherClassThatNeedsPDO = new otherClassThatNeedsPDO($pdo);

routes.php can hold the route definitions. If you are using this setup, you cannot directly link to files like how some beginner procedural setups are. You would need to send the requests to the public/index.php then come here to route against.

/public/index.php This is the only public PHP file (a server config). This can start the application, get the request, send it inward, then receive the response back. OR just call another file internally that does this - typically another bootstrap file.

In this example, I call the relevant files, then process the route request. This is almost identical to how the Slim Framework has it's skeleton application:

/public/index.php

<?php

declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php'; <-- Composer autoloader

require __DIR__ . '/../config/dependencies.php'; <-- class definitions

require __DIR__ . '/../config/routes.php'; <-- routes

Run the routes and emit the response.

  • Request / Response & HTTP:

https://symfony.com/doc/current/introduction/http_fundamentals.html

  • This is a Service class example using a library - to send domain-layer results to your user-interface layer, along with meta-data indicating the meaning of the domain results..

https://github.com/auraphp/Aura.Payload/blob/HEAD/docs/index.md#example

More on ADR (like MVC), same author as the MLAP book and above library (this uses a different version of that library) - https://github.com/pmjones/adr-example

  • A simple application in action using the Slim Framework (not really a framework):

https://github.com/slimphp/Tutorial-First-Application

Write up on this and adding ADR

https://www.slimframework.com/docs/v3/tutorial/first-app.html

https://www.slimframework.com/docs/v3/cookbook/action-domain-responder.html

preferably without mvc

That's a little difficult, most projects (not libraries) are done via a version of MVC.

MVC can be done procedurally too.

pseudo code to illustrate an idea.

parse_str($_SERVER['QUERY_STRING'], $qs);
$controller    = (string) $qs['controller'] ?? '';
$id            = (int) $qs['id'] ?? '';
$requestMethod = (string) $_SERVER['REQUEST_METHOD'];

// GET ?controller=post&id=1
switch ($controller) {
    case 'post': 
        switch ($requestMethod) {
            case 'GET':
                echo postControllerRead($id);
                break;
        }
        break;
}

function postControllerRead(int $id) {
    $data = getPostById($id);
    if (! $data) {
        // 404 header
        return templateRenderer('not-found.php');
    }
    return templateRenderer(
        'templates/post.php',
        ['post' => $data]
    );
}

function getPostById($id): array {
    global $conn; // FIX THIS LATER
    $sql = $conn->prepare("SELECT * FROM posts WHERE id = ?");
    $sql->execute([$id]);
    return $sql->fetch(PDO::FETCH_ASSOC); 
} 

function templateRenderer(string $file, array $data = []): string {
    ob_start();
    extract($data);
    require $file;
    return ob_get_clean();
}

Now change this to classes/objects:

// config/dependencies.php
$pdo = new PDO(...);
$postDatabase = new PostDatabase($pdo);
$template = new TemplateRenderer();
$postController = new PostController($postDatabase, $template);
$router = new Router(); // example from a library

// config/routes.php
// GET /post/1
// Using clean urls & a router library
$router->get('/post/{id}', function (int $id) use ($postController) {
    echo $postController->read($id);
});


class PostController {
    public function __construct(
        private PostDatabase $postDB,
        private TemplateRenderer $template
    ) {}

    public function read(int $id) {
        $data = $this->postDB->getById($id);
        if (! $data) {
            // 404 header
            return $this->template->render('not-found.php');
        }
        return $this->template->render(
            'templates/post.php',
            ['post' => $data]
        );
    }
}


class TemplateRenderer {
    function render(string $file, array $data = []): string {
        ob_start();
        extract($data);
        require $file;
        return ob_get_clean();
    }
}

// PostDatabase from the above comment