Grown Up PHP

Or

Please stop doing this:


$member = mysql_query("
   SELECT *
      FROM Users
      WHERE Email={$_GET['email']}
");
               

SilverStripe?

www.silverstripe.org

Or

Just use Silver Stripe

Hamish

That's my name

Don't wear it out

Why grow up?

Trust lets us participate in

solving important problems

So what don't I mean by grown up code?

"Serious" / "Enterprise"

  • Statically typed
  • Heavily isolated components
  • Pre-defined architecture
  • Waterfall development methodology

Problem?

  • Boring
  • Slow
  • Can't adapt to changing requirements

Also not what I mean: stereotypical "start-up"

  • Cowboy / plan-less
  • Very rapid development cycles
  • Velocity above all
  • Sell it now / dev it later
 
Young people are just smarter
 

— Mark Zuckerberg, 2007

Or

An old man complains about the young

Maybe they are though?

  • More likely, they just have different internal weighting to risk
  • Works fine until it doesn't

What do you mean by grown up code then?

Maturity
  • Clear comprehension of purpose
  • Self-knowledge leading to appropriate decision making
  • Respond to environment in an appropriate manner

Being grown up isn't ignorance or fear

It's walking into the wilds with your eyes wide open

Some things to be considering

Know your risks, and your resiliency

  • "Serious" development completely constrains risk by preventing all change
  • "Start-up" development completely ignores risk
  • But grown-up development is know your risks, document them all, then implement sensible constrains based on trade-offs

Think about the costs of your decisions over the entire lifetime of the project

  • Have a plan for support and maintenance
  • Consider having to pick a project back up after everyone who knows anything about it has gone
  • Are other people relying on a specific behaviour?

Be aware of your user's requirements and experience

  • What's the reason they're using your site?
  • Nobody likes bugs, but willingness to accept them varies
  • How will you know if there's something going wrong?

Know your own capabilities

  • You can't be a subject matter expert in "everything"
  • Sometime, an existing inelegant solution is better than a non-existant perfect one
  • You're never going to be able to do all the things you'd like to do

That sounds hard

And I'm very lazy

That's OK - there are some common things you can do to help

Or

Some things you should be using

(that maybe you aren't)

Agile development methodology

  • Scrum
  • Kanban

Scrum

  • Develop understanding of client's requirements by working with them on stories
  • Iterate in short sprints to complete a set of stories
  • Work closely with client at the end of every sprint to discuss requirements and trade-offs

Scrum tools

  • JIRA
  • Trello
  • About a million others

Frameworks

  • SilverStripe
  • Symfony 2
  • Laravel

Frameworks - outsource your responsibility

  • Security
  • Common structures and APIs
  • Reusable components and seperation of concerns
  • Integrated testing system

$member = mysql_fetch_assoc(mysql_query("
   SELECT *
      FROM Users
      WHERE Email={$_GET['email']}
"));
                  

$member = Member::get()
   ->filter('Email', $_GET['email'])
   ->first();
						

class BlogPost extends DataObject {
   static $db = array(
      'Post' => 'HTMLText'
   );
}
						

class BlogPost extends DataObject {
   static $db = array(
      'Post' => 'HTMLText'
      'Tags' => 'Text'
   );
}
						

Testing

  • PHPUnit
  • Behat
  • Phockito

Member:
   admin:
      FirstName: Admin
      Email: admin@silverstripe.com
						

class SecurityTest extends FunctionalTest {
   protected static $fixture_file
      = 'MemberTest.yml';

   public function testThatThing() {
      // Do the test
   }
}
						

Feature: Create a page
   As an author
   I want to create a page in the CMS
   So that I can grow my website

   @javascript
   Scenario: I can create a page from the pages section
      Given I am logged in with "ADMIN" permissions
      And I go to "/admin/pages"
      And I should see a "Add new" button in CMS Content Toolbar
      When I press the "Add new" button
      And I select the "Page" radio button
      And I press the "Create" button
      Then I should see an edit page form
						

Continuous integration

  • Travis
  • Teamcity
  • Jenkins

Continuous deployment?

Consistent and reproducible development environments

  • Puppet
  • Vagrant
  • Docker

Dependency management

  • Composer
  • Packagist
{
   "name": "silverstripe/cms",
   "type": "silverstripe-module",
   "description": "The SilverStripe Content Management System",
   "homepage": "http://silverstripe.org",
   "license": "BSD-3-Clause",
   "keywords": ["silverstripe", "cms"],
   "require": {
      "php": ">=5.3.2",
      "composer/installers": "*",
      "silverstripe/framework": "3.1.*"
   },
   "minimum-stability": "dev"
}
                  

Monitoring

  • Greylog2

Specifications

  • PSR-0, etc
  • Internal code style documentation

Language features

  • Exceptions
  • Name spaces
  • Improved expressibility

What have we learned?

  • Everything is acceptable except ignorance
  • Tools tools tools
  • You can be grown up and still have fun

The end