phpprocedural

37

Upload: fohi2009

Post on 15-Jan-2016

212 views

Category:

Documents


0 download

DESCRIPTION

php procedural

TRANSCRIPT

Page 1: phpprocedural
Page 2: phpprocedural

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: phpprocedural

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: phpprocedural
Page 5: phpprocedural

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

Page 6: phpprocedural

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: phpprocedural

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: phpprocedural

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

Page 9: phpprocedural

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: phpprocedural

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

Page 11: phpprocedural

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

Page 12: phpprocedural
Page 13: phpprocedural

Decorator

Page 14: phpprocedural

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: phpprocedural

The power of applying object-oriented design to your application

Page 16: phpprocedural

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: phpprocedural
Page 18: phpprocedural
Page 19: phpprocedural
Page 20: phpprocedural

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: phpprocedural
Page 22: phpprocedural

Hides actual implementation Reduces or isolates dependencies to

lower levels Reduces or eliminates extensive future

changes in code base

Page 23: phpprocedural

› 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: phpprocedural

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

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

› File attachments can be easy with function

Page 25: phpprocedural

Did you notice the dependencies?

Page 26: phpprocedural

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

Use dependency injection to expose dependencies

Page 27: phpprocedural

Pass connection into constructor and store it as a class property

Replace mysql_* functions with standard functions from PDO using connection

Page 28: phpprocedural

Database adapters are interchangeable

Page 29: phpprocedural

Interchangeable code components› Standard interface› Single dependency

Separate “engine” from implementation

Page 30: phpprocedural

Different payment processors are a simple drop-in replacement

Page 31: phpprocedural

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: phpprocedural
Page 33: phpprocedural
Page 34: phpprocedural

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

Page 35: phpprocedural

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: phpprocedural

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: phpprocedural