PHP Reset


Jordi Boggiano   @seldaek

http://nelm.io/

Disclaimer

This is not about REST

About Me

Belgian living in Zürich

Weby stuff for 10 years
http://seld.be

Symfony2 core dev and other OSS contributions
http://github.com/Seldaek

Recently started Nelmio
http://nelm.io
We do Symfony2 & Frontend Performance consulting

Planning

A bit of history

PHP4 - 22-May-2000

class Foo_Bar_Baz
{
    var $foo;

    function Foo_Bar_Baz()
    {
    }

    function bar()
    {
    }
}
            

PHP5 - 13-Jul-2004

class Foo_Bar_Baz
{
    public $foo;

    public function __construct()
    {
    }

    private function bar()
    {
    }
}
            

Could you spot the differences?

Yet seven years later
~20% of servers still run PHP4


Source: @iliaa's PHP Advent article, December 2010.

PHP5.3 - 30-Jun-2009

namespace Foo\Bar;

class Baz
{
    public $foo;

    public function __construct()
    {
    }

    private function bar()
    {
    }
}
            

Again, not much has changed.
But it changes everything.

Backslash\Means\No\Backwards\Compatibility

We've been sitting on our asses for too long.

It's time for a change.

New Frameworks

Symfony2

Lithium

Silex

ZF2

...

.

New Libraries

Assetic

Asset management & processing

http://github.com/kriswallsmith/assetic

Doctrine2

DBAL, ORM, ODMs

http://github.com/doctrine

Monolog

Logging

http://github.com/Seldaek/monolog

Twig

Template engine

http://github.com/fabpot/Twig

Gaufrette

Filesystem abstraction

http://github.com/knplabs/Gaufrette

Snappy

Generates PDF/images from webpages

http://github.com/knplabs/snappy

Imagine

Image manipulation

http://github.com/avalanche123/Imagine

Buzz

HTTP requests

http://github.com/kriswallsmith/Buzz

Goutte

Web Crawler

http://github.com/fabpot/Goutte

I can't list them all.


GitHub

Git will hurt you

Read http://progit.org - it's free.

GitHub is your friend

Best Practices

MIT License

Free as in free.

Autoload with PSR-0

// One class per file
// First bit should be a vendor name (your project, company, ..)
// This should sit in <project>/Nelmio/Bar/Baz.php
// WATCH THE CASE!

namespace Nelmio\Bar;

class Baz
{
}
            
http://tinyurl.com/psr0-standard

PEAR-style Coding Standards

namespace Foo\Bar;

class Baz
{
    public $foo;

    public function __construct()
    {
        $stuff = array('Coding Style');
        if (true) {
            foreach ($stuff as $blob) {
                echo "happy $blob!";
            }
        } else {
            echo "dead kittens";
        }
    }
}
            
http://pear.php.net/manual/en/standards.php

Remember, kids, standards
are NOT about taste

Testing

Composer

Package Management for PHP

Compose great things with small libraries

composer.json (lib)

{
    "name": "Monolog",
    "description": "Logging for PHP 5.3",
    "keywords": ["log","logging"],
    "homepage": "http://github.com/Seldaek/monolog",
    "version": "1.0.0",
    "license": "MIT",
    "authors": [
        {
            "name": "Jordi Boggiano",
            "email": "j.boggiano@seld.be",
            "homepage": "http://seld.be"
        }
    ],
    "require": {
        "php": ">=5.3.0"
    }
}

composer.json (project)

{
    "repositories": {
        "AwesomeLib": {
            "git": "git://github.com/Jim/AwesomeLib.git"
        },
        "GreatLib": {
            "git-bare": {
                "url": "git://github.com/Bob/GreatLib.git",
                "version": "1.0.0"
            }
        }
    },
    "require": {
        "Twig": ">=1.0.0",
        "AwesomeLib": "latest",
        "GreatLib": "1.0.0",
        "Monolog": "latest"
    }
}

Demo, kind of.
(blame @naderman :)

Fear code re-use no more.

Last words

Namespace adoption, in effect, opens up a world of possibilities.

Open up your mind,
we're not alone.

Share your code
and collaborate.

Thank you.

Feedback please on:

http://joind.in/3489

Slides:

http://slides.seld.be

Contact me:

@seldaek || jordi@nelm.io