What practice is better for my MVC? launch app through the __construct or use an initialise/run function?

Hi,

A silly question tbh but i’m currently rebuilding a 2020 up-to-date MVC from the ground up and tbh have been so busy with work that my PHP skills are in real need of a polish. I’ve been looking at CodeIgniter 4 and a bunch of other projects and i’m now unsure what is better practice? Do i load my application through the construct as i have done before or do i have a run function and intialise function like Codeigniter or a bunch of other sources i found online? Thanks

<?php

namespace MVC;

class App
{
    protected $controller = '';
    protected $method = '';
    protected $route = [];
    protected $url = [];
    protected $params = [];

    public function __construct()
    {
        #run app from here
    }
}

index.php

require APP . '/../app/init.php';

$app = new MVC\App();

init.php
init.php

if (PHP_VERSION_ID < 70400) {
    die('MVC requires minimum PHP version 7.4 (Current Version: ' . PHP_VERSION_ID . ')');
}

require_once 'core/App.php';

or

<?php

namespace MVC;

class App
{
    protected $controller = 'home';
    protected $method = 'index';
    protected $route = [];
    protected $url = [];
    protected $params = [];

    public function __construct()
    {
    }

    public function initialise()
    {
        echo 'u made it to initialise';
    }

    public function run()
    {
        $test = 'i went to the beach with dave';
        echo 'u accessed a public function' . $this->dashesToCamelCase($test, true);
    }
}

index.php

$app = require APP . '/../app/init.php';

$app->run();

init.php

if (PHP_VERSION_ID < 70400) {
    die('MVC requires minimum PHP version 7.4 (Current Version: ' . PHP_VERSION_ID . ')');
}

require_once 'core/App.php';
$app = new MVC\App();
$app->initialise();

return $app;

Thanks.

Using the constructor is better IMO because that way it’s impossible to create an object in an invalid state. If for some reason it would get called before its initialize method was called you’d get interesting results that are really hard to debug.

1 Like

Ahh ok thanks so much. What would u say is the reason CodeIgniter 4 are doing it through an intialise and run function? Thanks. Just want to fully understand.

    public function init()
    {
        $this->config = Config::getInstance();
        $this->request = Request::getInstance();
        $this->view = View::getInstance();
        if ($this->config->database->enabled) {
            $this->db = Database::getInstance();
        }
        if ($this->config->session->enabled) {
            $this->session = Session::getInstance();
        }
        if ($this->config->language->enabled) {
            $this->lang = Language::getInstance();
        }
        if ($this->config->application->autoload_composer) {
            require ROOT . "/vendor/autoload.php";
        }
        if ($this->config->autoload->enabled) {
            foreach ($this->config->autoload->helpers as $file) {
                require APP . "/Helpers/{$file}.php";
            }
            foreach ($this->config->autoload->libraries as $file) {
                $libname = "\\App\\Libraries\\" . ucfirst($file);
                $this->lib[$file] = new $libname();
            }
        }
    }

    public function run()
    {
        if (file_exists("{$this->request->controller_absolute_path}.php")) {
            require "{$this->request->controller_absolute_path}.php";
            $object = new $this->request->controller_object_name();
            if (method_exists($object, $this->request->method_name)) {
                $this->injectVariables($object);
                try {
                    call_user_func_array([$object, $this->request->method_name], (array)$this->request->arguments);
                    return;
                } catch (\Throwable $e) {
                    $this->setError(500, "{$e->getMessage()} on {$e->getFile()} line {$e->getLine()}");
                }
            }
        }

        # Unhandled request's being the 404.
        $this->setError(404);
    }

No idea :man_shrugging:

2 Likes

Oh dear me, I see you’ve edited your post.
What they’re using there is the singleton pattern. That would explain why they’re using an init function.

I can only recommend to stay as far away from it as possible and use Dependency Injection instead.

Singleton as nice as long as it works. Once the project gets big enough it will turn against you and you’ll have a horrible time fighting it. Better not to start with it in the first place.

2 Likes

thanks so much. appreciate your time and for explaining well.

I did not want to distract from the topic but are you sure you are looking at CI4 code? CI4 still has initialize methods for some reason (probably holdover from ancient times) but the code you posted does not match. I assume it is actually your code.

sorry ye the code i posted was from the other examples i was looking at using similar functionality to CI4. it was just in a more simple form so i used that here rather than CI.