Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

1 <3 Speed & Throwable

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Hey guys! I hate to start a tutorial off on a depressing note but... look... PHP 5 is dead. WAY dead. Like, it's not even supported anymore. PHP 5 is like that old, bad relationship that you just can't get out of. Hey, it's time to move on. You're better than PHP 5.

Also... did you know that PHP 7 is a full 2 numbers higher than PHP 5? I heard they skipped PHP 6 because 7 was just too awesome to fit into such a low number. Or... maybe because they messed up PHP 6. Anyways, let's pretend that it's because PHP 7 is so great... because it is.

This tutorial is about learning the important stuff... only. Look, you can spend hours reading the PHP 7 CHANGELOG. Believe us... we did it. I get it, they made a lot of stuff better... cool... but most of it isn't that critical. This means that we will not be talking about the spaceship operator... because it is apparently not a person who drives a flying saucer. Nope, it's an edge-case way to compare numbers and strings. So disappointing.

Speed Sells

So what is important in PHP 7? Honestly... the biggest selling point for upgrading is speed. PHP 7 performance is on point. To show it off, Zend made a cute infographic. Summary: PHP 7 equals zoooooom!

And that means you can take this to your manager and say:

Hey buddy! When we upgrade to PHP 7, our pages will be faster and we can turn off like 10 servers.

And then they'll throw you a parade and promote you to CEO. Enjoy.

Setting up the Project

And now that we know how to sell the upgrade to management, let's get into the cool technical stuff.

As always, you should definitely enjoy a snack during the tutorial... and code along with me! Download the course code from this page and unzip it. Inside, you'll find a fancy start/ directory with the same code you see here. Follow the README.md file to get things set up. The last step will be to find your favorite terminal, go to the project directory, and run:

php bin/console server:run

to start the built-in web server. Open up the project in your browser: http://localhost:8000.

Welcome to AquaNote! A project we've been building in our Symfony series. For this tutorial, it'll be a nice skeleton to start with.

Proper Error Handling

Ok, the first feature of PHP 7 that gets me excited is... proper error handling. Stay with me: I promise this is exciting!

At the root of the project, create a new file called play-exceptions.php. Inside, let's do something fun: like, write some really bad code! We'll call an undefinedFunction(). And below, I'll write "continue processing file", even though we know that's crazy! This script will blow up way before that line.

<?php
undefinedFunction();
echo "\n\nContinue processing file...";

Find your terminal, open up a new tab, and run:

php play-exceptions.php

Fatal error! Woohoo!

Catching Errors

In PHP 5, we could catch exceptions... but not errors. Sure, you could try to work with the error handler... but that's confusing stuff! In PHP 7... they fixed things! We can finally catch errors.

Start with a normal try-catch block. But instead of catching Exception, catch \Throwable. Yes! In PHP 7, you can write bad code, catch it, and even print out the error message!

<?php
try {
undefinedFunction();
} catch (\Throwable $error) {
echo 'Now if you write bad code, you can catch it! ' . $error->getMessage();
}
echo "\n\nContinue processing file...";

Try the file again:

php play-exceptions.php

Haha! It actually runs.

About Throwable

About this Throwable thingy: it's actually a core interface. Here's the deal: we still have the core Exception class. And now, there is also an Error class. And both Exception and Error implement the Throwable interface. So if you want to catch both exceptions and errors, catch \Throwable. If you want to only catch exceptions, then catch \Exception. And if you want to only catch errors, use catch \Error. It's quite elegant.

And, just like with exceptions, there are different types of errors, each with its own class. For example, TypeError is thrown when you're passing an argument of a wrong type to a function. And actually, that's our next topic: the new scalar type system and strict mode!

Leave a comment!

13
Login or Register to join the conversation
Default user avatar
Default user avatar Mesut Vatansever | posted 5 years ago
6 Reply
Petr fort Avatar
Petr fort Avatar Petr fort | posted 3 years ago

Hi, I have some "problems/warnings/errors" with php 7.3, but after "composer update", everything is perfect, hope it helps,
Petr

1 Reply
Griff P. Avatar
Griff P. Avatar Griff P. | posted 3 years ago

Hi, I tried to follow the setup instructions for this course but hit a problem trying to run the server. When I run php bin/console server:run after running composer install, I get

[Symfony\Component\Console\Exception\CommandNotFoundException]
There are no commands defined in the "server" namespace.

And running php bin/console shows no server commands.
What should I do? I am taking this tutorial to learn PHP7 - I do not have much knowledge of Symfony (yet...).

Reply

Hey Griff P.

I guess you're on Symfony5, if that's the case, the webserver-bundle does not come by default, you can whether install it or spin up the webserver by using the Symfony CLI. If you already have it installed, then just run symfony --help to check all the commands (you'll have to run symfony serve -d)

Cheers!

Reply
Griff P. Avatar

Hi Diego - I have unpacked the supplied course code into an empty directory, and so the version of Symfony I am running is the one that results from running 'composer install' in the /start directory that was provided as part of the course materials. Running bin/console about after the Composer install tells me I am running Version 3.3.18. Try it yourself - unpack the course code from the website into an empty directory, run 'composer install' and then try running 'bin/console server run', as given in the instructions in the README file. Does it work for you, or do you get the same error that I reported (and am still seeing)? Would it be possible for you to give me some more detailed instructions on how to get the server running, please? (I am running PHP 7.4.5 on OSX Mojave.)

Reply

Hey Griff P.

The course code was missing the web-server-bundle dependency. I just fixed that. If you download again the course code, the bin/console server:run command should work. If you don't want to do that, you can require that dependency by yourself, just follow this steps

1) run composer require symfony/web-server-bundle "3.3.*" --dev
2) Add this line $bundles[] = new Symfony\Bundle\WebServerBundle\WebServerBundle(); inside the file app/AppKernel.php``

// app/AppKernel.php

class AppKernel extends Kernel
{

public function registerBundles()
{
    $bundles = array(
        ...
    );

    if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {
        $bundles[] = new Symfony\Bundle\WebServerBundle\WebServerBundle();
        ...
    }

    return $bundles;
}
...

}



Cheers!
1 Reply
Griff P. Avatar
Griff P. Avatar Griff P. | MolloKhan | posted 3 years ago | edited

Hi MolloKhan
Thank you so much for taking the time to make this fix. It works great now. Very much appreciated. I love SymfonyCasts - it's a fantastic resource. Cheers!

1 Reply

Hi, after typing php bin/console server:run, I get this error message on my terminal:
unable to find file @frameworkbundle/resources/config/router_dev.php

Reply

Hi @Flo

Can you please provide more information about your environment: Operation System name, PHP version?

Cheers!

Reply

Hi @sadikoff ,
thanks for your reply. I use Windows 8.1 and PHP 7.1.16

My other Symfony project that I created from scratch or downloaded are working. Only that one from the tutorial is not.

Cheers!

Reply
sadikoff Avatar sadikoff | SFCASTS | Flo | posted 4 years ago | edited

Can you please:

  • remove vendor/ directory
  • run composer install
  • run server with php bin/console server:run

if there is still an issue than:

  • re-download course code
  • unzip it
  • go to start directory
  • run composer install
  • run server again php bin/console server:run

Cheers!

Reply

Thanks, I tried but still the same error. I can view the webpage for 2mins and then it shows me the error.

Reply

Hm, this is really weird! Could you please say which PHP extensions have you activated?

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

This tutorial uses Symfony 3, but all the concepts around PHP 7 are still ?valid.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.3.*", // v3.3.18
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.10.3
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.2
        "symfony/swiftmailer-bundle": "^2.3", // v2.5.4
        "symfony/monolog-bundle": "^2.8", // v2.12.1
        "symfony/polyfill-apcu": "^1.0", // v1.3.0
        "sensio/distribution-bundle": "^5.0", // v5.0.19
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.25
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "knplabs/knp-markdown-bundle": "^1.4", // 1.5.1
        "doctrine/doctrine-migrations-bundle": "^1.1", // v1.2.1
        "stof/doctrine-extensions-bundle": "^1.2", // v1.2.2
        "composer/package-versions-deprecated": "^1.11" // 1.11.99
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.1.4
        "symfony/phpunit-bridge": "^3.0", // v3.2.8
        "nelmio/alice": "^2.1", // v2.3.1
        "doctrine/doctrine-fixtures-bundle": "^2.3", // v2.4.1
        "symfony/web-server-bundle": "3.3.*"
    }
}
userVoice