Você está na página 1de 37

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

Most scripts & tutorials all procedural


or show bad understanding of OO
PHPs 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

That was easy!


Add Object-Oriented
PHP to your resume!

Its much more than just syntax Its a


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

possible

Where have we seen this before?

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

Create meaning and structure


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

Code objects cannot always represent


real-world objects
Application flow
Structure
Delegation
Separation of responsibilities

How do we solve these problems?


Design Patterns

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

What OO programming is not


where many PHP projects go wrong

Decorator

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

purpose

bob is a User
1 instance = 1
user
Properties of a
user

The power of applying objectoriented design to your application

Objects add automatic clarity


Stick to standards and naming
conventions
camelCase
Zend Coding Standards

Function naming as verbose as possible


getPostBySlug($slug)
findPostsWithTag($tagName)

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

Hides actual implementation


Reduces or isolates dependencies to
lower levels
Reduces or eliminates extensive future
changes in code base

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

Custom Email() object


Clear, descriptive syntax
Unlimited configuration options
mail(), SMTP, database, external server, etc.
File attachments can be easy with function

Did you notice the


dependencies?

Remove the database dependencies by


using a standard interface
PDO provides one (bundled with PHP5)

Use dependency injection to expose


dependencies

Pass connection into constructor


and store it as a class property

Replace mysql_* functions


with standard functions
from PDO using connection

Database adapters
are interchangeable

Interchangeable code components


Standard interface
Single dependency

Separate engine from


implementation

Different payment processors


are a simple drop-in
replacement

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

Building a Data Mapper with PHP5 and


the Standard PHP Library
Friday Oct. 10 @ 10:30am

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

Vance Lucas, 23
Blog: www.vancelucas.com
Email: vance@vancelucas.com

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

Você também pode gostar