php - procedural to object-oriented

37

Upload: vance-lucas

Post on 25-Jan-2015

18.687 views

Category:

Technology


3 download

DESCRIPTION

Learn the power of object-oriented programming in PHP5 and the many benefits it offers over the more traditional PHP procedural programming style. This session will include a light introduction to object-oriented concepts and will provide real-world concrete examples of the benefits it can offer you and the PHP projects you work on.

TRANSCRIPT

Page 1: PHP - Procedural To Object-Oriented
Page 2: PHP - Procedural To Object-Oriented

The Current Situation Object-Oriented Introduction

› Basics, concepts, terminology & examples Object-Oriented Benefits

› Code re-use, portability, structure, meaning, control, separation of responsibility, time, reduce dependencies and more

Page 3: PHP - Procedural To Object-Oriented

Most scripts & tutorials all procedural or show bad understanding of OO

PHP’s unique history› Template language & PHP/FI 2.0› “evolved” over time with OO features

Good OO finally starting to emerge› PHP5 released in 2004› Zend Framework› Symfony Project

Page 4: PHP - Procedural To Object-Oriented
Page 5: PHP - Procedural To Object-Oriented

That was easy!Add “Object-Oriented PHP” to your resume!

Page 6: PHP - Procedural To Object-Oriented

It’s much more than just syntax – It’s a mindset

Object-Oriented programming gives meaning to your data› Represents real-world objects when

possible

Where have we seen this before?

Page 7: PHP - Procedural To Object-Oriented

Semantic markup Think:

› <blockquote> for quotes, not indentation› <address> for addresses, not <p> or

<div>› <cite> instead of <i> for citations

Goal is to for code to convey meaning

Page 8: PHP - Procedural To Object-Oriented

• Create meaning and structure• Separate responsibilities• Reduce/Isolate dependencies• Create common public interfaces• Clear, self-documenting code

Page 9: PHP - Procedural To Object-Oriented

Code objects cannot always represent real-world objects› Application flow› Structure› Delegation› Separation of responsibilities

How do we solve these problems?› Design Patterns

Page 10: PHP - Procedural To Object-Oriented

Model-View-Controller (MVC) Active Record Data Mapper Adapter Registry Factory Observer

Page 11: PHP - Procedural To Object-Oriented

What OO programming is not – where many PHP projects go wrong

Page 12: PHP - Procedural To Object-Oriented
Page 13: PHP - Procedural To Object-Oriented

Decorator

Page 14: PHP - Procedural To Object-Oriented

What is User?› A finder/utility class?› No clear definition or

purpose

“bob” is a User 1 instance = 1

user Properties of a

user Functions of a user

Page 15: PHP - Procedural To Object-Oriented

The power of applying object-oriented design to your application

Page 16: PHP - Procedural To Object-Oriented

Objects add automatic clarity Stick to standards and naming

conventions› camelCase› Zend Coding Standards

Function naming as verbose as possible› getPostBySlug($slug)› findPostsWithTag($tagName)

Page 17: PHP - Procedural To Object-Oriented
Page 18: PHP - Procedural To Object-Oriented
Page 19: PHP - Procedural To Object-Oriented
Page 20: PHP - Procedural To Object-Oriented

Parent > Child relationship between objects› Child extends Parent

Child inherits all parent methods› Child can override parent methods when

required Changes can be reflected across your

entire application with relative ease

Page 21: PHP - Procedural To Object-Oriented
Page 22: PHP - Procedural To Object-Oriented

Hides actual implementation Reduces or isolates dependencies to

lower levels Reduces or eliminates extensive future

changes in code base

Page 23: PHP - Procedural To Object-Oriented

› Adding extra headers is a mess (CC, BCC)› No built-in file attachment options› Limited configuration options

(Sendmail/SMTP)› Very inefficient for large amounts of email

Page 24: PHP - Procedural To Object-Oriented

› Custom Email() object› Clear, descriptive syntax› Unlimited configuration options

mail(), SMTP, database, external server, etc.

› File attachments can be easy with function

Page 25: PHP - Procedural To Object-Oriented

Did you notice the dependencies?

Page 26: PHP - Procedural To Object-Oriented

Remove the database dependencies by using a standard interface› PDO provides one (bundled with PHP5)

Use dependency injection to expose dependencies

Page 27: PHP - Procedural To Object-Oriented

Pass connection into constructor and store it as a class property

Replace mysql_* functions with standard functions from PDO using connection

Page 28: PHP - Procedural To Object-Oriented

Database adapters are interchangeable

Page 29: PHP - Procedural To Object-Oriented

Interchangeable code components› Standard interface› Single dependency

Separate “engine” from implementation

Page 30: PHP - Procedural To Object-Oriented

Different payment processors are a simple drop-in replacement

Page 31: PHP - Procedural To Object-Oriented

Objects should serve a single purpose› Separate code into manageable chunks

MVC – Good example› Model – Database› View – Display› Controller – Request/Flow

Resist temptation of globals everywhere› “global $var”, $_SESSION, $_REQUEST, etc

Page 32: PHP - Procedural To Object-Oriented
Page 33: PHP - Procedural To Object-Oriented
Page 34: PHP - Procedural To Object-Oriented

Building a Data Mapper with PHP5 and the Standard PHP Library› Friday Oct. 10 @ 10:30am

Page 35: PHP - Procedural To Object-Oriented

www.php.net/php5› Full official PHP5 feature documentation

Communities› Sitepoint – sitepoint.com› PHPit – phpit.com› PHP|Architect – phparch.com› Zend Developer Zone - devzone.zend.com

Page 36: PHP - Procedural To Object-Oriented

Vance Lucas, 23› Blog: www.vancelucas.com› Email: [email protected]

Making websites since age 12› Started learning PHP when PHP3 was new

Oklahoma Baptist University – Shawee› Bachelor of Business Administration

Currently work at Back40 Design, Inc.› www.back40design.com

Page 37: PHP - Procedural To Object-Oriented