Chapters
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
SOLID: The Good, The Bad & The Real World
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Hey friends! Welcome to our long awaited tutorial on the principles of SOLID: single responsibility principle, open closed principle, Liskov substitution principle, interface segregation principle and, my personal favorite: the donut in face principle. Probably... actually known as the dependency inversion principle.
I want to thank my coauthor Diego for helping me finally put this tutorial together. And I'm super sorry if you've been waiting for this!
SOLID Principles: I don't Love Them
So... why did it take us so long to get this tutorial done? The short answer is: I.... kind of don't like the SOLID principles. Okay, let me rephrase that. The SOLID principles are tough to understand. And, in my most humble opinion, they're not always good advice! It depends on the situation. For example, you should write code for your application differently than you would write code that's meant to be open sourced and shared.
If you want to know a bit more about why SOLID might not always be correct, you can read a recent blog post written by Dan North called CUPID – THE BACK STORY. Dan North is known for being the person who first made behavior-driven development famous. You may have heard of him if you're a Behat user.
Anyways, this tutorial is not going to be yet another tutorial where we read the definition of each SOLID principle in a monotone voice... and slowly get lost, bored and finally fall asleep. Nope. We're going to dive into each principle, learn what they really mean - using normal human words - code some real examples and discuss why and when following these principles makes sense and does not make sense. But even when the SOLID principles should not be followed, they have a lot to teach us. So strap in for a wild ride.
Project Setup
Since we're going to be doing some real coding, let's get the project set up and rocking. Do me a solid by downloading the course code from this page and unzipping it. After you do, you'll find a start/
directory with the same code you see here. This fancy README.md
file has all the details about how to get the project up and running. The last step will be to find a terminal, move into the project and start a local web server. I'll use the Symfony binary for this:
symfony serve -d
Once this finishes, copy that URL, spin back over to your browser, paste and... say hello to "Sasquatch Sightings"! Our latest effort to find the infamous Bigfoot. What this code actually does is... not too important. It talks to a database, lists some big foot sightings and has some calculations. It will be our playground for diving into the SOLID principles.
So next, let's start with the first: the single responsibility principle!
12 Comments
I swear I heard SALAD too xD xD but anyways, those guys always makes the topic fun and interesting xD
SOLID and SALAD have one thing in common, both things are good for you :)
I am getting a syntax error when trying to load the database.
I have done the following:
- Downloaded the code
- Run Composer
- Update the .env file for postgresl
- Run the 3 symfony console command
The problem I get is when it tries to insert the data in to the 'user' table. I can't seem to find the file that has the user data or sql commands to insert the data.
Hey @JDCrain
Could you tell me what error did you get? To insert the data you need to load the fixtures (after providing the database). Run bin/console doctrine:fixtures:load
- if you're on Docker run symfony console doctrine:fixtures:load
Cheers!
I am not using Docker but I have tried both 'bin/console' and 'symfony console' and get the same error about executing the 'INSERT INTO' commands.
Here is a screenshot of the output after running the command.
Ohh, I think it is a Postgres thing. Try surrounding the name of the table user
in backticks ( ` ). You'll need to add this to the User entity class
#[ORM\Table(name: '`user`')]
class User {}
Or, if that does not work, try renaming the table to something else. The problem is that user
is a reserved word
Cheers!
That worked. I had to change the comment a little since this course is using the 5.2.6 version of Symfony.
I do everything it says on the README.md but when i try to run symfony console doctrine:database:create
i get this error: In AbstractMySQLDriver.php line 112: An exception occurred in driver: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)<br />SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)<br />
The Mysql service is running. and when I connect to mysql in my terminal using mysql -u root -p
i don't get any errors
Hey @The-cocktail-E
Did you update the database connection environment variable? DATABASE_URL
you can do so by creating a .env.local
file
Cheers!
ARM architecture is not working fine with mysql 5.7 but this is the solution.
Run this command on terminal docker pull --platform linux/x86_64 mysql<br />
Update the docker-compose.yaml file like that :
version: '3.7'
services:
database:
image: 'mysql:5.7'
platform: linux/amd64
environment:
MYSQL_ROOT_PASSWORD: password
ports:
# To allow the host machine to access the ports below, modify the lines below.
# For example, to allow the host to connect to port 3306 on the container, you would change
# "3306" to "3306:3306". Where the first port is exposed to the host and the second is the container port.
# See https://docs.docker.com/compose/compose-file/#ports for more information.
- '3306'
Now run
docker-compose up -d
and
symfony console doctrine:database:create
symfony console doctrine:schema:update --force
symfony console doctrine:fixtures:load
Hey @GolemAI
Thank you for sharing it! Cheers!
"Houston: no signs of life"
Start the conversation!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99.1
"doctrine/annotations": "^1.0", // 1.12.1
"doctrine/doctrine-bundle": "^2", // 2.3.1
"doctrine/doctrine-migrations-bundle": "^3", // 3.1.1
"doctrine/orm": "^2", // 2.8.4
"knplabs/knp-time-bundle": "^1.15", // v1.16.0
"phpdocumentor/reflection-docblock": "^5.2", // 5.2.2
"sensio/framework-extra-bundle": "^6.0", // v6.1.2
"symfony/console": "5.2.*", // v5.2.6
"symfony/dotenv": "5.2.*", // v5.2.4
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "5.2.*", // v5.2.6
"symfony/framework-bundle": "5.2.*", // v5.2.6
"symfony/http-client": "5.2.*", // v5.2.6
"symfony/mailer": "5.2.*", // v5.2.6
"symfony/property-access": "5.2.*", // v5.2.4
"symfony/property-info": "5.2.*", // v5.2.4
"symfony/security-bundle": "5.2.*", // v5.2.6
"symfony/serializer": "5.2.*", // v5.2.4
"symfony/twig-bundle": "5.2.*", // v5.2.4
"symfony/validator": "5.2.*", // v5.2.6
"symfony/webpack-encore-bundle": "^1.6", // v1.11.1
"symfony/yaml": "5.2.*", // v5.2.5
"twig/cssinliner-extra": "^3.3", // v3.3.0
"twig/extra-bundle": "^2.12|^3.0", // v3.3.0
"twig/twig": "^2.12|^3.0" // v3.3.0
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.2", // 3.4.0
"fakerphp/faker": "^1.13", // v1.14.1
"symfony/debug-bundle": "^5.2", // v5.2.4
"symfony/maker-bundle": "^1.13", // v1.30.2
"symfony/monolog-bundle": "^3.0", // v3.7.0
"symfony/stopwatch": "^5.2", // v5.2.4
"symfony/var-dumper": "^5.2", // v5.2.6
"symfony/web-profiler-bundle": "^5.2" // v5.2.6
}
}
SALAD :)