Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/willavelar/php-solid-single-responsibility-principle
- Owner: willavelar
- Created: 2023-08-23T17:47:28.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-23T17:53:41.000Z (about 1 year ago)
- Last Synced: 2023-08-23T19:18:48.019Z (about 1 year ago)
- Topics: php, solid, solid-principles
- Language: PHP
- Homepage:
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.