Public or private by default, what to choose?

When I worked on another project, I met an interesting opinion. They say, all the code in the project should be as open as possible by default. I.e. we should make all new methods and fields public, and we should not put final anywhere, except some special cases when it's really necessary. It was motivated with the difficulties of further codebase support:

If I want to inherit the class, and there is a private method, I have to study the code to understand why it was made private and whether I can make it public.

This opinion was very unusual for me, and I decided to figure it out if there is really more problems when fields and methods are private by default.
Let's write an extra public method and see how easy is it to support it.

interface FooInterface {
  public function foo();
}

class Foo implements FooInterface {
  public function foo() {
    // do stuff
    $this->bar();
    // do stuff
  }

  public function bar() {
    // do stuff
  }
}

April 30, 2023
About 3 min
Don't do this: nonexistent trait fields usage

With this article, I open a rubric "Don't do this." It will contain code from real projects, from which I will remove all references to the projects themselves. And, of course, my explanations of why shouldn't you do this, and the way it should be done.

So, the first patient. Inside the trait, the fields of its descendants are used. I mean something like this:

trait Foo
{
    public function bar(): string
    {
        return $this->field . static::CONSTANT . $this->method();
    }
}

Note

Or even worse, $this->{$attribute}. But it's out of the question, as the chance of getting an error 500 rises to almost 100%.


April 23, 2023
About 2 min
An example of setting up xDebug in docker

Once upon a time, I put together a working environment with PHP, xDebug, Docker and PhpStorm. Since then, I have been dragging it from project to project and felt happy. For those who find it difficult to set up a local environment with Docker and xDebug, I am posting this config with explanations. Here below we will create a Docker image with xDebug installed, configure PhpStorm and explain a working configuration for Docker Compose.

xDebug setup example in docker

January 21, 2023
About 3 min
How to use PHP without installing it (Linux)

The answer is definitely simple: use Docker 😃 Benefits I get from using PHP like this:

  • No need to bother with setting up a local environment, switching PHP versions, installing additional libraries and resolving conflicts
  • One text file with command line aliases is enough for any version of PHP to work on any computer. And from here
    • Ease of transferring data between machines
    • You can safely demolish the system on your main computer, restoring work with PHP will be as simple as git pull

Note

This article is about parsing the use of docker in one specific scenario, so if you are already familiar with this technology, you can safely skip it. The rest of you are welcome to the further reading.

How to use PHP without installing it

August 19, 2022
About 5 min