Note that there are some explanatory texts on larger screens.

plurals
  1. POAlternatives to static methods in a framework PHP
    primarykey
    data
    text
    <p>Lately I have been trying to create my own PHP framework, just to learn from it (As we may look into some bigger and more robust framework for production). One design concept I currently have, is that most core classes mainly work on static functions within classes.</p> <p>Now a few days ago, I've seen a few articles about "Static methods are death to testability". This concerned me as.. yeah.. my classes contain mostly static methods.. The main reason I was using static methods is that a lot of classes would never need more than one instance, and static methods are easy to approach in the global scope. Now I'm aware that static methods aren't actually the best way to do things, I'm looking for a better alternative.</p> <p>Imagine the following code to get a config item:</p> <pre><code>$testcfg = Config::get("test"); // Gets config from "test" echo $testcfg-&gt;foo; // Would output what "foo" contains ofcourse. /* * We cache the newly created instance of the "test" config, * so if we need to use it again anywhere in the application, * the Config::get() method simply returns that instance. */ </code></pre> <p>This is an example of what I currently have. But according to some articles, this is bad. <br/> Now, I could do this the way how, for example, CodeIgniter does this, using:</p> <pre><code>$testcfg = $this-&gt;config-&gt;get("test"); echo $testcfg-&gt;foo; </code></pre> <p>Personally, I find this harder to read. That's why I would prefer another way.</p> <p><strong>So in short</strong>, I guess I need a better approach to my classes. I would not want more than one instance to the config class, maintain readability and have easy access to the class. Any ideas?</p> <p>Note that I'm looking for some best practice or something including a code sample, not some random ideas. Also, if I'm bound to a $this->class->method style pattern, then would I implement this efficiently?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload