Modern PHP: New Features and Good Practices

I recently read Modern PHP: New Features and Good Practices and I thought I would pass along several things that I learned while reading the book.

Overview

Modern PHP: New Features and Good Practices is “modern” up to around PHP 5.6 so it doesn’t cover any of the new 7.0 features (it actually recommends Hack near the end). It does a good job of pushing the idea that you should be using composer to load in libraries that do what you need instead of reinventing the wheel which I love. I would recommend anyone who has a basic PHP understanding as it covers a lot of helpful topics that may not be covered in other sources.

1. Traits

Traits are an interesting feature in PHP. They allow you to define a structure then reuse the methods and properties of the trait in multiple classes and have multiple traits in the same class. This is a work around to the fact that PHP doesn’t support multiple inheritance but it seems to work REALLY well.

trait nameTrait 
{
    protected $name = null;

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }
}

class person
{
    use nameTrait;
}

$aPerson = new Person();
$aPerson->setName('Scott Keck-Warren');
echo $aPerson->getName();

Output:

Scott Keck-Warren

2. Generators

Generators allow you create dynamic iterators without needing to create a class that supports the iterator interface. I find this to be a interesting concept for abstracting away large data sets but I haven’t been able to wrap my head around a really good use case. The power behind this feature comes from the yield keyword which returns the value but causes execution of the yielded function to restart when the next value is needed.

function itemGenerator()
{
    for ($i = 1; $i<12; $i++) {
        yield "Item $i";
    }
}

foreach (itemGenerator() as $item){
    echo $item, PHP_EOL;
}

Output:

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
Item 11

3. Built in Server Has Drawbacks

PHP has a built in server so you can test your sites.

php -S localhost:8000
PHP 5.5.27 Development Server started at Mon Mar 14 21:07:43 2016
Listening on http://localhost:8000
Document root is /Users/scottkeckwarren/Projects/jekyll.tpt.com
Press Ctrl-C to quit.

I’ve never used it because I prefer the VM experience but it turns out there are actually problems that would prevent people from using it:

  1. Only one request at a time
  2. Limited mime types
  3. Limited URL rewriting

Just more reason to us VMs with a full Apache install.

4. https://github.com/ziadoz/awesome-php

https://github.com/ziadoz/awesome-php is a list of good PHP libraries and resources. I wasn’t even aware this existed until I read about it in the book so it will be my goto resource for libraries.

5. https://github.com/filp/whoops

PHP errors for cool kids

https://github.com/filp/whoops

This looks like a cool project to clean up the errors that PHP displays and I’ll be integrating it into my development environment.

6. XHProf

XHProf is a profiler developed by Facebook like Xdebug but it can run on production environments without a huge performance hit.

Conclusion

Overall, I really enjoyed reading Modern PHP: New Features and Good Practices. I think it would be a good book to read once but I’m not sure I’ll ever refer back to it.