Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/willavelar/php-solid-single-responsibility-principle

a simple php example about the first principle of SOLID
https://github.com/willavelar/php-solid-single-responsibility-principle

php solid solid-principles

Last synced: 6 days ago
JSON representation

a simple php example about the first principle of SOLID

Awesome Lists containing this project

README

        

## Single responsibility principle

This principle of SOLID basically says that a class should have only one responsibility.

It says that when we have a class that does not meet this principle, we must divide it into more classes until this occurs.

We'll create a small class to manage our reports.

```php
$this->getTitle(),
'date' => $this->getDate(),
];
}

public function formatJson()
{
return json_encode($this->getContents());
}
}
```

In this example, our class has two responsibilities:

Bring in the report data.
Format the report to JSON format. So we are violating the principle of single responsibility.
To resolve this, let's do the following:
```php
$this->getTitle(),
'date' => $this->getDate(),
];
}
}
```
```php
getContents());
}
}
```

Now a new class called JsonReportFormatter has been created, exclusively responsible for formatting the report in JSON.