PHP MVC Config class as dependency or with just class constants, accessible from overall?

In my MVC project (PHP 7.1) I have a Config class and a multitude of config files, each of them looking like this:

return [
    'web' => [
        'host' => 'localhost',
        //...
    ],
];

At the app entry point I make an instance of the Config class and load all config file arrays in it. The created object is then passed as constructor argument in all classes that needs it:

class AbstractView {

    private $config;

    public function __construct(Config $config) {
        $this->config = $config;
    }

    private function prepareContext() {
        $this->assign('appHost', $this->getConfig()->get('web/host'));
    }

}

Because it’s quite a “big effort” to pass the Config object quite overall in my app, I thought of implementing aConfig` class with only class constants in it, like:

class Config {

    const WEB_PROTOCOL = 'http';
    const WEB_HOST = 'localhost';

}

and access them from overall directly with

class AbstractView {

    private function prepareContext() {
        $this->assign('appHost', Config::WEB_HOST);
    }

}

I wanted to ask: Is this a really good alternative, having in mind that I want to completely avoid static states in my app? Is this alternative a “static” one or absolute not? What about testability (I don’t have any experience with it yet)?

Thank you very much.


Source: stackoverflow-php

classes which have dependencies other other classes

I’m looking for some direction regarding the following, I’m new to OOP and getting there but think either my lack of understanding is causing me to get stuck in a rabbit hole or I’m just over thinking things too much and being anal.

basically i have a main class called “CurlRequest” which sole purpose is to perform curl requests, providing a url and params it returns me some html. This class works and functions as intended and I’m happy with that.

I use this class for a few projects but for one I then wanted to track the performance of my requests made. attempted, failed, passed etc, so i created a static class for this which manages all my counters. I place counter references like the following at different areas in my CurlRequest class.

PerformanceTracker::Increment('CurlRequest.Attempted');
PerformanceTracker::Increment('CurlRequest.Passed');
PerformanceTracker::Increment('CurlRequest.Failed');

I have around 10 or so of these with my class tracking all kinds of things during the curl request and i also use my PerformanceTracker class in other classes i made.

However like mentioned i only wanted to do this for one of my projects, so find my self in the situation of having my original CurlRequest class and an altered one with performance counters in it.

My question is, is their a way i can use the same class for any project and choose to use the PerformanceTracker class or not. The obvious way i thought of was to pass an $option argument into the class and then have if statements around all the counters, but can’t help think its messy.

            if ($this->options['perfCounter'] == true ) {

                PerformanceTracker::Increment($this->owner . '.CurlRequest.Failed');

            }

this also adds a lot of extra code to the class.


Source: stackoverflow-php