5 Ways to Awesome-ize Your (PHP) Code

66
5 Ways to Awesome-ize Your (PHP) Code Jeremy Kendall Thursday, November 7, 13

description

5 principles you can apply *right now* to make your code better. The principles apply to most languages, the implementation is PHP specific.

Transcript of 5 Ways to Awesome-ize Your (PHP) Code

Page 1: 5 Ways to Awesome-ize Your (PHP) Code

5 Ways to Awesome-ize Your (PHP) Code

Jeremy Kendall

Thursday, November 7, 13

Page 2: 5 Ways to Awesome-ize Your (PHP) Code

Thursday, November 7, 13

Page 3: 5 Ways to Awesome-ize Your (PHP) Code

I love to code

Thursday, November 7, 13

Page 4: 5 Ways to Awesome-ize Your (PHP) Code

I love to code

I’m terribly forgetful

Thursday, November 7, 13

Page 5: 5 Ways to Awesome-ize Your (PHP) Code

I love to code

I’m terribly forgetful

I take pictures

Thursday, November 7, 13

Page 6: 5 Ways to Awesome-ize Your (PHP) Code

I love to code

I’m terribly forgetful

I take pictures

I work at OpenSky

Thursday, November 7, 13

Page 7: 5 Ways to Awesome-ize Your (PHP) Code

What Are These 5 Ways of Which You Speak?

Thursday, November 7, 13

Page 8: 5 Ways to Awesome-ize Your (PHP) Code

Use Version Control

Thursday, November 7, 13

Page 9: 5 Ways to Awesome-ize Your (PHP) Code

Use Version Control

‣ If you aren’t using version control, use git

Thursday, November 7, 13

Page 10: 5 Ways to Awesome-ize Your (PHP) Code

Use Version Control

‣ If you aren’t using version control, use git

Install git

Thursday, November 7, 13

Page 11: 5 Ways to Awesome-ize Your (PHP) Code

Use Version Control

‣ If you aren’t using version control, use git

Install git

cd /path/to/project

Thursday, November 7, 13

Page 12: 5 Ways to Awesome-ize Your (PHP) Code

Use Version Control

‣ If you aren’t using version control, use git

Install git

cd /path/to/project

git init

Thursday, November 7, 13

Page 13: 5 Ways to Awesome-ize Your (PHP) Code

Use Version Control

‣ If you aren’t using version control, use git

Install git

cd /path/to/project

git init

git add .

Thursday, November 7, 13

Page 14: 5 Ways to Awesome-ize Your (PHP) Code

Use Version Control

‣ If you aren’t using version control, use git

Install git

cd /path/to/project

git init

git add .

git commit -m “Just saved the company”

Thursday, November 7, 13

Page 15: 5 Ways to Awesome-ize Your (PHP) Code

Use Version Control

‣ If you aren’t using version control, use git

Install git

cd /path/to/project

git init

git add .

git commit -m “Just saved the company”

‣ Interactive tutorial at http://try.github.io

Thursday, November 7, 13

Page 16: 5 Ways to Awesome-ize Your (PHP) Code

or Else . . .

Thursday, November 7, 13

Page 17: 5 Ways to Awesome-ize Your (PHP) Code

Error Reporting: Turn it Up!

Thursday, November 7, 13

Page 18: 5 Ways to Awesome-ize Your (PHP) Code

Error Reporting: Turn it Up!‣ Turn error reporting all the way up in dev

Thursday, November 7, 13

Page 19: 5 Ways to Awesome-ize Your (PHP) Code

Error Reporting: Turn it Up!‣ Turn error reporting all the way up in dev

‣ Helps find existing problems

Thursday, November 7, 13

Page 20: 5 Ways to Awesome-ize Your (PHP) Code

Error Reporting: Turn it Up!‣ Turn error reporting all the way up in dev

‣ Helps find existing problems

‣ Helps head off future pain

Thursday, November 7, 13

Page 21: 5 Ways to Awesome-ize Your (PHP) Code

Error Reporting: Turn it Up!‣ Turn error reporting all the way up in dev

‣ Helps find existing problems

‣ Helps head off future pain

‣ Don’t say it’s just an E_NOTICE . . .

Thursday, November 7, 13

Page 22: 5 Ways to Awesome-ize Your (PHP) Code

Error Reporting: Turn it Up!‣ Turn error reporting all the way up in dev

‣ Helps find existing problems

‣ Helps head off future pain

‣ Don’t say it’s just an E_NOTICE . . .

Thursday, November 7, 13

Page 23: 5 Ways to Awesome-ize Your (PHP) Code

Error Reporting: Dev

display_errors = Ondisplay_startup_errors = Onerror_reporting = -1log_errors = On

Thursday, November 7, 13

Page 24: 5 Ways to Awesome-ize Your (PHP) Code

Error Reporting: Prod

display_errors = Offdisplay_startup_errors = Offerror_reporting = E_ALLlog_errors = On

Thursday, November 7, 13

Page 25: 5 Ways to Awesome-ize Your (PHP) Code

Ditch NIH

Thursday, November 7, 13

Page 26: 5 Ways to Awesome-ize Your (PHP) Code

Ditch NIH

‣ If you write it all yourself . . .

Thursday, November 7, 13

Page 27: 5 Ways to Awesome-ize Your (PHP) Code

Ditch NIH

‣ If you write it all yourself . . .

‣ . . . you maintain it all yourself

Thursday, November 7, 13

Page 28: 5 Ways to Awesome-ize Your (PHP) Code

Ditch NIH

‣ If you write it all yourself . . .

‣ . . . you maintain it all yourself

‣ Eventually you have very little time for either . . .

Thursday, November 7, 13

Page 29: 5 Ways to Awesome-ize Your (PHP) Code

Real, Actual Work

Thursday, November 7, 13

Page 30: 5 Ways to Awesome-ize Your (PHP) Code

Real, Actual Work

Thursday, November 7, 13

Page 31: 5 Ways to Awesome-ize Your (PHP) Code

A Life

Thursday, November 7, 13

Page 32: 5 Ways to Awesome-ize Your (PHP) Code

Ditch NIH

Thursday, November 7, 13

Page 33: 5 Ways to Awesome-ize Your (PHP) Code

Ditch NIH

‣ Offload work to the open source community

Thursday, November 7, 13

Page 34: 5 Ways to Awesome-ize Your (PHP) Code

Ditch NIH

‣ Offload work to the open source community

‣ Install Composer (http://getcomposer.org)

Thursday, November 7, 13

Page 35: 5 Ways to Awesome-ize Your (PHP) Code

Ditch NIH

‣ Offload work to the open source community

‣ Install Composer (http://getcomposer.org)

‣ Search Packagist (http://packagist.org)

Thursday, November 7, 13

Page 36: 5 Ways to Awesome-ize Your (PHP) Code

Ditch NIH

‣ Offload work to the open source community

‣ Install Composer (http://getcomposer.org)

‣ Search Packagist (http://packagist.org)

‣ Add dependency to composer.json

Thursday, November 7, 13

Page 37: 5 Ways to Awesome-ize Your (PHP) Code

Ditch NIH

‣ Offload work to the open source community

‣ Install Composer (http://getcomposer.org)

‣ Search Packagist (http://packagist.org)

‣ Add dependency to composer.json

‣ Run composer install or composer update

Thursday, November 7, 13

Page 38: 5 Ways to Awesome-ize Your (PHP) Code

Ditch NIH

‣ Offload work to the open source community

‣ Install Composer (http://getcomposer.org)

‣ Search Packagist (http://packagist.org)

‣ Add dependency to composer.json

‣ Run composer install or composer update

‣ Everyone needs logging. Go install monolog.

Thursday, November 7, 13

Page 39: 5 Ways to Awesome-ize Your (PHP) Code

DRY Up Your DB

Thursday, November 7, 13

Page 40: 5 Ways to Awesome-ize Your (PHP) Code

DRY Up Your DB

‣ If you’re not using PDO, switch now.

Thursday, November 7, 13

Page 41: 5 Ways to Awesome-ize Your (PHP) Code

DRY Up Your DB

‣ If you’re not using PDO, switch now.

‣ If you’re not using prepared statements, switch now.

Thursday, November 7, 13

Page 42: 5 Ways to Awesome-ize Your (PHP) Code

DRY Up Your DB

‣ If you’re not using PDO, switch now.

‣ If you’re not using prepared statements, switch now.

‣ Replace connections and queries one at a time

Thursday, November 7, 13

Page 43: 5 Ways to Awesome-ize Your (PHP) Code

DRY Up Your DB

‣ If you’re not using PDO, switch now.

‣ If you’re not using prepared statements, switch now.

‣ Replace connections and queries one at a time

‣ (or one group at a time)

Thursday, November 7, 13

Page 44: 5 Ways to Awesome-ize Your (PHP) Code

DRY Up Your DB

‣ If you’re not using PDO, switch now.

‣ If you’re not using prepared statements, switch now.

‣ Replace connections and queries one at a time

‣ (or one group at a time)

‣ Combine with simple data access objects

Thursday, November 7, 13

Page 45: 5 Ways to Awesome-ize Your (PHP) Code

DRY Up Your DBclass UserDao{ protected $db;

public function __construct(\PDO $db) { $this->db = $db; }

public function find($id) { $sql = 'SELECT * FROM users WHERE id = :id'; $stmt = $this->db->prepare($sql); $stmt->bindValue(':id', $id); $stmt->execute();

return $stmt->fetch(); }}

Thursday, November 7, 13

Page 46: 5 Ways to Awesome-ize Your (PHP) Code

DRY Up Your DBclass UserDao{ protected $db;

public function __construct(\PDO $db) { $this->db = $db; }

public function find($id) { $sql = 'SELECT * FROM users WHERE id = :id'; $stmt = $this->db->prepare($sql); $stmt->bindValue(':id', $id); $stmt->execute();

return $stmt->fetch(); }}

Thursday, November 7, 13

Page 47: 5 Ways to Awesome-ize Your (PHP) Code

DRY Up Your DBclass UserDao{ protected $db;

public function __construct(\PDO $db) { $this->db = $db; }

public function find($id) { $sql = 'SELECT * FROM users WHERE id = :id'; $stmt = $this->db->prepare($sql); $stmt->bindValue(':id', $id); $stmt->execute();

return $stmt->fetch(); }}

Thursday, November 7, 13

Page 48: 5 Ways to Awesome-ize Your (PHP) Code

DRY Up Your DBclass UserDao{ protected $db;

public function __construct(\PDO $db) { $this->db = $db; }

public function find($id) { $sql = 'SELECT * FROM users WHERE id = :id'; $stmt = $this->db->prepare($sql); $stmt->bindValue(':id', $id); $stmt->execute();

return $stmt->fetch(); }}

Thursday, November 7, 13

Page 49: 5 Ways to Awesome-ize Your (PHP) Code

DRY Up Your DBclass UserDao{ protected $db;

public function __construct(\PDO $db) { $this->db = $db; }

public function find($id) { $sql = 'SELECT * FROM users WHERE id = :id'; $stmt = $this->db->prepare($sql); $stmt->bindValue(':id', $id); $stmt->execute();

return $stmt->fetch(); }}

Thursday, November 7, 13

Page 50: 5 Ways to Awesome-ize Your (PHP) Code

Or Just Pick Something from Packagist

Thursday, November 7, 13

Page 51: 5 Ways to Awesome-ize Your (PHP) Code

Start Writing Beautiful Code

Thursday, November 7, 13

Page 52: 5 Ways to Awesome-ize Your (PHP) Code

Start Writing Beautiful Code‣ It’s time for a coding standard!

Thursday, November 7, 13

Page 53: 5 Ways to Awesome-ize Your (PHP) Code

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

Thursday, November 7, 13

Page 54: 5 Ways to Awesome-ize Your (PHP) Code

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

‣ Pick someone else’s

Thursday, November 7, 13

Page 55: 5 Ways to Awesome-ize Your (PHP) Code

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

‣ Pick someone else’s

‣ Use automated tools to enforce

Thursday, November 7, 13

Page 56: 5 Ways to Awesome-ize Your (PHP) Code

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

‣ Pick someone else’s

‣ Use automated tools to enforce

‣ php-cs-fixer

Thursday, November 7, 13

Page 57: 5 Ways to Awesome-ize Your (PHP) Code

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

‣ Pick someone else’s

‣ Use automated tools to enforce

‣ php-cs-fixer

‣ PHP_CodeSniffer

Thursday, November 7, 13

Page 58: 5 Ways to Awesome-ize Your (PHP) Code

Start Writing Beautiful Code‣ It’s time for a coding standard!

‣ Makes life so much easier

‣ Pick someone else’s

‣ Use automated tools to enforce

‣ php-cs-fixer

‣ PHP_CodeSniffer

‣ Refactor bit-by-bit

Thursday, November 7, 13

Page 59: 5 Ways to Awesome-ize Your (PHP) Code

Bonus!

Thursday, November 7, 13

Page 60: 5 Ways to Awesome-ize Your (PHP) Code

PHP: The Right Way

Thursday, November 7, 13

Page 61: 5 Ways to Awesome-ize Your (PHP) Code

PHP: The Right Way

‣ Go to http://www.phptherightway.com/

Thursday, November 7, 13

Page 62: 5 Ways to Awesome-ize Your (PHP) Code

PHP: The Right Way

‣ Go to http://www.phptherightway.com/

‣ Start reading

Thursday, November 7, 13

Page 63: 5 Ways to Awesome-ize Your (PHP) Code

PHP: The Right Way

‣ Go to http://www.phptherightway.com/

‣ Start reading

‣ Don’t stop reading

Thursday, November 7, 13

Page 64: 5 Ways to Awesome-ize Your (PHP) Code

PHP: The Right Way

‣ Go to http://www.phptherightway.com/

‣ Start reading

‣ Don’t stop reading

‣ Do it “The Right Way” for 6 months

Thursday, November 7, 13

Page 65: 5 Ways to Awesome-ize Your (PHP) Code

PHP: The Right Way

‣ Go to http://www.phptherightway.com/

‣ Start reading

‣ Don’t stop reading

‣ Do it “The Right Way” for 6 months

‣ Then pick and choose

Thursday, November 7, 13

Page 66: 5 Ways to Awesome-ize Your (PHP) Code

Thanks!

[email protected]

http://about.me/jeremykendall

@jeremykendall

http://365.jeremykendall.net

Thursday, November 7, 13