SlideShare une entreprise Scribd logo
1  sur  135
Télécharger pour lire hors ligne
최태리
<?OOps!
The PHP Fear and
Loathing Guide to Basic
Object-Oriented Design
terry chay
tagged
11 February 2009
engineering brownbag
최태리
{introduction
2XtremePeople
최태리
“PhpPatterns is just that—a source of
design patterns for PHP code. Will
wonders never cease? I ran this by a Java
programmer I know that I have drawn
into the PHP world and her comment
was, ‘I’d have never even thought to try
patterns in PHP.’”
—the FuzzyBlog
phppatterns? {
object-orientedsnob
{
{
Web Developer Training
PHP to ASP.NET
{
최태리
“ASP.NET applications are based on a robust
Object Oriented Programming (OOP) paradigm
rather than a scripting paradigm…The upside of
ASP.NET’s support of OOP concepts [vs PHP]
means that ASP.NET applications for the most
part run better designed code, have clear
separation of content, logic, and data and thus
are generally easier to support over the long
term.”
—Migrating from PHP to ASP.NET
dotFUD {
최태리
“Although being a young fellow myself, I
have to hold my hand up and say I was
educated in an era before design patterns
were even invented. Yes it’s true, such a
time did exist. In my day, it was called
‘Data Structures’.”
—Alan Williamson, Editor-in-Chief
November 2002
datastructures? {
grumpyoldprogrammer
{
the middle way
{
離
11守
破
threelevels
{
離
12守
破
shuhold
chinese character “house”
and “law”
to abide by; to defend
the o-o bigot
7 habits: dependence
{
離
13守
破
habreak
chinese character “stone”
+ phonetic
to break
the grumpy old
programmer
7 habits: independence
{
離
14守
破
rileave
chinese character “bird” +
phonetic
to leave; to depart
the middle way
7 habits: interdependence
{
Some basic O-O
{
16
myquiz
1:<?php
2:$tests[1] = new stdClass();
3:$tests[2] = new stdClass();
4:
5:foreach ($tests as $obj) {
6: $obj->foo = 'bar';
7:}
8:
9:print_r($tests);
10:?>
1.
2.
3.
tychay$ php test.php
Array
(
[1] => stdClass Object
(
)
[2] => stdClass Object
(
)
)
{
1. construct some generic
PHP objects
2.interate over objects and
assign them a value
3.dump the array of objects
what do you think the
output of this is and why?
UNEXPECTED!
1:<?php
2:$tests[1] = new stdClass();
3:$tests[2] = new stdClass();
4:
5:foreach ($tests as $obj) {
6: $obj->foo = 'bar';
7:}
8:
9:print_r($tests);
10:?>
17
myquiz
remember that PHP4,
objects are like everything
else, so you need the &!
Otherwise it will use a
copy of the object.
{
1:<?php
2:$tests[1] =& new stdClass();
3:$tests[2] =& new stdClass();
4:
5:foreach ($tests as $count=>$obj) {
6: $tests[$count]->foo = 'bar';
7:}
8:
9:print_r($tests);
10:?>
18
myquiz
tychay$ php answer1.php
Array
(
[1] => stdClass Object
(
[foo] => bar
)
[2] => stdClass Object
(
[foo] => bar
)
)
now we’ve fixed the
problem!
{
19
byreference
{
remember this is true also
when:
prints empty object!
1:<?php
2:class testClass
3:{
4: function addObject(&$object)
5: {
6: $this->object = $object;
7: }
8:}
9:$testObject =& new testClass();
10:
11:function addBar($object)
12:{
13: $object->bar = 'this is set also';
14:}
15:addBar($testObject);
16:
17:function getSameObject()
18:{
19: global $testObject;
20: return $testObject;
21:}
22:$returnObject = getSameObject();
23:$returnObject->same = 'this should be in object';
24:
25:class testRefClass
26:{
27: function testRefClass($objFromHell)
28: {
29: $objFromHell->something = 'something here';
30: $objFromHell->addObject($this);
31: }
32:}
33:$tempObject = new testRefClass($testObject);
34:print_r($testObject);
1. passing parameters
3. constructor
2. functions that return
objects
20
{1:<?php
2:class testClass
3:{
4: function addObject(&$object)
5: {
6: $this->object = $object;
7: }
8:}
9:$testObject =& new testClass();
10:
11:function addBar(&$object)
12:{
13: $object->bar = 'this is set also';
14:}
15:addBar($testObject);
16:
17:function &getSameObject()
18:{
19: global $testObject;
20: return $testObject;
21:}
22:$returnObject =& getSameObject();
23:$returnObject->same = 'this should be in object';
24:
25:class testRefClass
26:{
27: function testRefClass(&$objFromHell)
28: {
29: $objFromHell->something = 'something here';
30: $objFromHell->addObject($this);
31: }
32:}
33:$tempObject =& new testRefClass($testObject);
34:print_r($testObject);
byreference
remember this is true also
when:
1. passing parameters
3. constructor
2. functions that return
objects
PHP5 fixes this!
최태리
“…the new object model makes object
oriented programming in PHP much
more powerful and intuitive. No longer
will you have to mess with cryptic &
characters to get the job done. No longer
will you have to worry about whether
changes you made to the object inside
the constructor will survive the dreaded
new-operator behavior…
php5 {
최태리
…No longer will you ever have to stay up
until 2:00AM tracking elusive bugs!”
—Zeev Suraski
November 2002
php5 {
Zeev’s2AMbug
최태리
23
staticproperties
no static class variables in PHP.
substitute with
global variables
static function variables (bad idea)
{
24
globalstatic
bind by reference in
constructor (or elsewhere)
1:<?php
2:$GLOBALS['_statics']['testClass']['staticCount'] = 0;
3:class testClass
4:{
5: var $staticCount;
6: function testClass()
7: {
8: $this->staticCount =& $GLOBALS['_statics']
['testClass']['staticCount'];
9: }
10: function increment()
11: {
12: return ++$this->staticCount;
13: }
14:}
15:$test1 =& new testClass();
16:$test2 =& new testClass();
17:$test1->increment();
18:$test2->increment();
19:print_r($test1);
20:print_r($test2);
21:echo testClass::increment();
22:?>
tychay$ php test3.php
testclass Object
(
[staticCount] => 2
)
testclass Object
(
[staticCount] => 2
)
1
watch out though!
25
singleton
one of the simplest
patterns
creates a single instance
of a class (DB
connection)
lazy evaluation
flexible
1:<?php
2:class testSingleton
3:{
4: function &instance()
5: {
6: static $singleton;
7: if (!$singleton) {
8: $singleton =& new testSingleton();
9: }
10: return $singleton;
11: }
12:}
13:$test1 =& testSingleton::instance();
14:$test1->foo = bar;
15:$test2 =& testSingleton::instance();
16:print_r($test2);
17:?>
tychay$ php test4.php
testsingleton Object
(
)
watch out though!
26
singleton
also note the liberal use
of &
1:<?php
2:$GLOBALS['_statics']['testSingleton']['singleton'] = null;
3:class testSingleton
4:{
5: function &instance()
6: {
7: if (is_null($GLOBALS['_statics']['testSingleton']
['singleton'])) {
8: $GLOBALS['_statics']['testSingleton']['singleton']
=& new testSingleton();
9: }
10: return $GLOBALS['_statics']['testSingleton']
['singleton'];
11: }
12:}
13:$test1 =& testSingleton::instance();
14:$test1->foo = bar;
15:$test2 =& testSingleton::instance();
16:print_r($test2);
17:?>
tychay$ php test5.php
testsingleton Object
(
[foo] => bar
)
27
latebinding
when a polymorphic call
is resolved at run time
(instead of compile time)
oo-bigots advocate
against (speed, strong
typing)
simple to do in PHP
1:<?php
2:class testLateBinding
3:{
4: function callMeLate()
5: {
6: echo "I was calledn";
7: $this->wasCalled = true;
8: }
9:}
10:$object1 =& new testLateBinding();
11:$object2 =& new testLateBinding();
12:$functionname = 'callMeLate';
13:
14:$object1->{$functionname}();
15:
16:call_user_func(array(&$object2,$functionname));
17:print_r($object2);
18:?>
tychay$ php test6.php
I was called
I was called
testlatebinding Object
(
[wasCalled] => 1
)
variable-variables
call_user_func()
PHP4.3+
note the &!.
{
28
一
三
二
roadmap
29
一
三
二
the why
why do old metaphors
fail?
why use agile processes?
methodology
30
一
三
二
the what
what are the precepts of
class rules
what are the parameters
of package design
what are patterns?
principles
31
一
三
二
the how
how do patterns allow
me to follow principles?
how can patterns work
around a design error?
how do patterns apply to
more than O-O?
patterns
최태리
methodology
一
최채리
metaphors
一
一
34
codecomplete
Published 1993
Microsoft “Tome”
Award winning book
35
paradigm
shift
Code Complete is a “wanna
be” Structure of Scientific
Revolutions
he uses “paradigm shift”
Key premise: key
metaphor of software
should be construction
一
최태리
36
straw men
The metaphor of
construction is so central
to the book, that the
subtitle is: “A Practical
Handbook of Software
Construction.”
Thus it needs to
construct some straw
men of failed metaphors
in order to prove why
we should adopt the
“software as
construction” paradigm.
一
최태리
37
一 1.software
penmanship
“writing code”
causes expensive trial and error
focused on originality > reuse
“plan to throw one away”
“it might have been state-of-the-art
software-engineering practice in 1975”
2.softwarefarming
一
최태리
39
growing a system “piece by piece”
bad metaphor!
fertilizing a system plan?
thinning the detailed design?
increasing code yields through effective
land managment?
harvesting code?
rotating in a crop of assembler?
Who ever believed this shit?
一
최태리
why Code Complete sucks
software as
construction
一
The #1 reason why
software is not
construction
一
離
守
破
Mythical
Man Month
This is a really famous essay
from 1975. Does the year
sound familiar? Yes, the
Frederick Brooks is the same
engineer treated with derision
in Code Complete’s “Software as
Writing” straw man.
A lot of what he says was
controversial, but one thing
that was never denied was the
man-month was a myth.
What is a mythical man
month?
一
43
man-month
一the premise is that men
and months are
interchangeable.
This means that in order
to reach a deadline I
simply add people to the
project.
최태리
44
man-month
myth
The problem:
Adding people to a late
project makes it later!
first consider case where
they’re everything is
partition-able (man-
month).
then add constant time
for training
then add
communication: n(n-1)/2
compare with
unpartitionable (single
man)
최채리
45
where?
man-hour (măn’our')
n.
An industrial unit of production equal
to the work one person can produce
in an hour.
Source: The American Heritage®
Dictionary of the English Language,
Fourth Edition
fromconstruction!
一
programming!=labor
최태리
46
Mythical Man-Month referred to five
times in Code Complete
Including having the author give an
endorsement on the back cover!
irony
php{con
최채리
최태리
47 최채리
“I’d be astonished if the open-source
community has in total done as many
man-years of computer security code
reviews as we have done in the last two
months”
—Steven Lipner, Director of Security
Assurance, Microsoft (April 8th, 2002)
persistantmyth
asengineering
software…
一
최태리
49
“The final goal of any engineering
activity is some type of
documentation. When a design effort
is complete, the design documentation
is turned over to the manufacturing
team. If the design documents truly
represent a complete design, the
manufacturing team can proceed to
build the product.”
—Jack Reeves, C++ Journal, 1992
一
최태리
50
“Two Irreparable Mistakes of thre
Software Field: 1. The believe that the
source code is a development product,
not the design for a product.”
—Michael Feathers, ObjectMentor
The other problem with software as
construction:
the “design” phase finishes with
working source code
it’s based on the illusion that the
source code is the product!
一
최태리
51
How did we buy this illusion?
“It is cheaper and simpler to build the
design and test it than do anything else.
We do not care how many builds we do
—they cost next to nothing…No other
modern industry would tolerate a
rework rate of over 100% in its
manufacturing process”
—Jack Reeves, C++ Journal, 1992
a software “build” means
“manufacturing” phase is cheap.
一
최태리
離
守
破
A software developer is more akin to an
artist than an assembly line worker…
Throwing more people into the design
mix can be counterproductive.
—Jason Burkert
“Software as engineering” accepts the
reality of the mythical man-month!
The source code is the design.
離
一
53
roadmap
一
三
二
now that we’ve talked
discussed the paradigm or
metaphor it is time to apply
that to programming
practice.
54
heavyweight
離
守
破
methodologies like
ISO9001 violate
“software as an
engineering”
by definition
treat auxiliary
documentation as the
product
55
Note influence of
construction metaphor
BDUF: Big Design Up
Front
Big Bang Delivery
waterfall
SYSTEM
SPECIFICATION
REQUIREMENTS
ANALYSIS
ARCHITECTURAL
DESIGN
CODING AND
DEBUGGING
DETAILED DESIGN
UNIT TESTING
SYSTEM TESTING
MAINTAINENCE
from Code Complete, p.3
BDUF
Big Bang
一
56
changing requirements
constant release schedule
not life-death
“The reason why dynamic
languages like Perl, Python,
and PHP are so important …
Unlike applications from the
previous paradigm, web
applications are not released
in one to three year cycles.
They are updated every day,
sometimes every hour.”
—Tim O’Reilly, 14 May 2003
websoftware
一
최태리
57
“The biggest mistake in ‘Build one to
throw away’ concept is that it implicitly
assumes the classical sequential or
waterfall model of software
construction.”
—Frederick Brooks, Mythical Man-
Month, 20th Aniv. 1995
moreirony
一
agility
一
59
XP
離
守
破
ship early and often!
one of many lightweight
methodologies known as
“agile”
I’ll cover three features
of XP. So you get an idea
of what is considered an
“agile” process
一
1.Test-First Design
一
designbytest
recall the waterfall process.
Look at where the unit
testing is.
recall the “software as
engineering” metaphor: tests
and builds are cheap. Why
aren’t we leveraging it?
We should be testing first and
using testing as a design
technique (the hard part of
engineering), not a testing
technique (easy part).
SYSTEM
SPECIFICATION
REQUIREMENTS
ANALYSIS
ARCHITECTURAL
DESIGN
CODING AND
DEBUGGING
DETAILED DESIGN
UNIT TESTING
SYSTEM TESTING
MAINTAINENCE
一
1:<?php
2:class MoneyBagTest extends TestCase {
3:    var $m12CHF;
4:    var $m14CHF;
5:    var $m28USD;
6:    var $mArray1;
7:    var $moneybag1;
8:    var $moneybag2;
9: function MoneyBagTest( $name = "MoneyBagTest" ) {
10: $this->TestCase( $name );
11: }
12:
13: function setUp() {
14: $this->m12CHF = new Money( 12, "CHF" );
15: $this->m14CHF = new Money( 14, "CHF" );
16: }
17: function tearDown() {
18: $this->m12CHF = NULL;
19: $this->m14CHF = NULL;
20: $this->m28USD = NULL;
21: }
22:
23: function testSimpleAdd() {
24: $result = $this->m12CHF->add( $this->m14CHF );
25: $this->assert(new Money(27,"CHF"), $result, "This
should fail" );
26: $this->assertEquals(new Money(27,"CHF"), $result,
"This should fail" );
27: }
28:
29: // insert more tests here: use "test..." for name of
function.
30:}
62
code unit test
first
1. Code the Unit Test
First
2. Write minimum code
until test works
3. Refactor (see later)
4. Rerun tests
constructor
a test
一
fixture
keep the bar green to keep the code clean…
31:
32:$test = new MoneyBagTest();
33:$testRunner = new TestRunner();
34:$testRunner->run($test);
35:?>一
2.Refactoring
一
최태리
65
“The process of changing a software
system in such a way that does not alter
the external behavior of the code yet
improves the internal structure”
—Martin Fowler, Refactoring, 1999
the changes by themselves are tiny
focus on simplicity (in design) not
simplistic (implementation)
refactorit!
一
최태리
66
do this continuously
purpose of a module of code:
function it performs
allow change
communicate
prevents code rot in large projects
my experience.
refactorit!
一
3.You Aren’t Going to Need
It (YAGNI)
一
최태리
68
Implement the simplest thing that
could possibly work.
Basically, if you need it, you can put it
in later—it does not “save time.”
Set aside your fears about tomorrow
and concentrate on today.
whatisYAGNI?
최태리
69
YAGNIperks
keeps system smaller and easier to
understand (form of refactoring)
more likely to ship out the door if you
focus on needs
allows for system accretion
recall how fast a web iteration vs. Big
Bang
최태리
70
“A project done in Java will cost 5
times as much, take twice as
long, and be harder to maintain
than a project done in a scripting
language such as PHP or Perl,”
—Philip Greenspun
20 September 2003
최태리
71
lack of rigid O-O definitely a plus!
recall the “Migrating from PHP to
dotNET” document
lack of framework definitely a plus!
phpYAGNI
72
Well bad pattern for a
loosly typed language
such as PHP!
Iterator Pattern: provide
a way to access the
element of an aggregate
object sequentially
without exposing its
underlying
representation
badpattern
Aggregate
CreateIterator()
ConcreteAggregate
+ CreateIterator()
Iterator
+ First()
+ Next()
+ IsDone()
+ CurrentItem()
ConcreteIterator
return new ConcreteIterator(&$this)
1:<?php
2://A lot of code in here...
3:include(ECLIPSE_ROOT.'ArrayIterator.php');
4:
5:$iterator =& new ArrayIterator(&$arrayToBeIterated);
6:while ($element=$iterator->getCurrent()) {
7: echo $iterator->key().' => '.$element;
8: $iterator->next();
9:}
10:$iterator->reset();
11:
12:?>
iterator# phpdefault
You can vary what is being
iterated over (ignoring
constructor)
YAGNI!
If you must have an
iterator there is SPL in
PHP5!
1:<?php
2:foreach($arrayToBeIterated as $key=>$value) {
3: echo $key.' => '.$value;
4:}
5:?>
최태리
74
pair programming
the planning game/user-stories
code reviews
acceptance tests
on-site customer
other processes: SCRUM, Crystal, etc.
code ownership
XPnotcovered
一
75
roadmap
一
三
二
the metaphor/paradigm for
building software is
engineering
we now know agility: the
lightweight processes that
avoid the waterfall process
離
守
破
76
roadmap
一
三
二
What programming
principles help us program
correctly?
최태리
principles
二
78
roadmap
一
三
二
1. What precepts for class
design?
2. Grouping classes into
packages using what
parameters?
3. What are patterns?
precepts of classes
二
최태리
80
single
responsibility
A class should have only one reason to
change.
Each responsibility is an axis of
change.
more than one responsibility =
coupling
二
81
violation
PEAR is great.
Except…
This is the PEAR Error
constructor. Note that
this method causes the
object to violate SRP!
2:function PEAR_Error($message = 'unknown error', $code =
null,
3: $mode = null, $options = null, $userinfo = null)
4:{
5: if ($mode === null) {
6: $mode = PEAR_ERROR_RETURN;
7: }
8: $this->message = $message;
9: $this->code = $code;
10: $this->mode = $mode;
11: $this->userinfo = $userinfo;
12: if (function_exists("debug_backtrace")) {
13: $this->backtrace = debug_backtrace();
14: }
15: if ($mode & PEAR_ERROR_CALLBACK) {
16: $this->level = E_USER_NOTICE;
17: $this->callback = $options;
18: } else {
19: if ($options === null) {
20: $options = E_USER_NOTICE;
21: }
22: $this->level = $options;
23: $this->callback = null;
24: }
25: if ($this->mode & PEAR_ERROR_PRINT) {
26: if (is_null($options) || is_int($options)) {
27: $format = "%s";
28: } else {
29: $format = $options;
30: }
31: printf($format, $this->getMessage());
32: }
33: if ($this->mode & PEAR_ERROR_TRIGGER) {
34: trigger_error($this->getMessage(), $this->level);
35: }
82
and the violation goes on
callbacks
die
return mode
print
trigger_error
exception
36: if ($this->mode & PEAR_ERROR_DIE) {
37: $msg = $this->getMessage();
38: if (is_null($options) || is_int($options)) {
39: $format = "%s";
40: if (substr($msg, -1) != "n") {
41: $msg .= "n";
42: }
43: } else {
44: $format = $options;
45: }
46: die(sprintf($format, $msg));
47: }
48: if ($this->mode & PEAR_ERROR_CALLBACK) {
49: if (is_string($this->callback) && strlen($this-
>callback)) {
50: call_user_func($this->callback, $this);
51: } elseif (is_array($this->callback) &&
52: sizeof($this->callback) == 2 &&
53: is_object($this->callback[0]) &&
54: is_string($this->callback[1]) &&
55: strlen($this->callback[1])) {
56: @call_user_func($this->callback, $this);
57: }
58: }
59: if (PEAR_ZE2 && $this->mode &
PEAR_ERROR_EXCEPTION) {
60: eval('throw $this;');
61: }
62:}
63:?>
최태리
83
Liskov substitution
Subtypes must be substitutable for
their base types.
not an “is-a” relationship
validation of LSP occurs in the client!
(Design By Contract).
二
최태리
84
Design By Contract
validates Liskov
Substituion. What
validates Design by
contract?
Unit Tests! Remember
them? They determine
define it.
In fact, PEAR PHPUnit
can document a Design
By Contract by reading
the unit test (aka
TestDox).
1:<?php
2:require_once 'PHPUnit/Framework/TestCase.php';
3:
4:class FooTest extends PHPUnit_Framework_TestCase
5:{
6:    public function testIsASingleton() {}
7:    public function
testAReallyLongNameIsAGoodThing() {}
8:}
9:?>
Foo
- Is a singleton
- A really long name is a good thing
I violate LSP
Look at how naïve this
diagram is. It’s the most
obvious one but focuses on
the physical objects instead of
their behaviors.
The objects are defined in
terms of the clients. Think
about an image coming back
from the camera, or trying to
set a dimmer to a light switch
that turns on or off only.
Unfortunately I had to live
with it for reasons later.
Common
CameraSwitchable
Sensor
HVAC
최태리
86
otherprecepts
Open-Closed Principle
Dependency-Inversion Principle
Interface Segregation Principle
二
parameters of packages
二
최태리
88
“A few days ago, I was at Microsoft, and
they held an amazing conference…After
this conference, I was kinda depressed
about the future of PHP and Apache…
The ASP.NET stuff is just perfect. Fast,
Secure and Robust. ASP.NET
technology is better than PHP
technology.”
—Sterling Hughes
5 June 2003
二
최태리
Common language Runtime
access to COM+
server ui controls
“code behind” (.cs)
server-side object model
event-driven handlers
automatic page state (ViewState)
built-in Validation object
try-catch
二
최태리
built-in authentication (Web.config)
built-in database abstraction (ADO.NET) +
connection pooling + data binding
built-in MSXML: DOM, XSLT, SAX + Web
Services
auto targeting of HTML
IDE (Visual Studio .NET)
built-in output caching
I see what Sterling is saying!
However…
二
최태리
Common Language Runtime 2nd class, compiling cache, Pint (Parrot)
access to COM+ access to COM, Java, C/C++ extensions,
mono, command line
server ui controls,“ViewState”- saving
form state
PEAR::HTML_* (Form, Menu, Page…) +
others + BYO
“code behind” smarty, fasttemplate, flexy, it, phplib, +
others, BYO
server-side object model session, __sleep(), __wakeup(), SRM
event driven handlers bad idea, fusebox + zillions of others,
s9y, BYO
output-caching zend accelerator, Cache, Cache_Lite,
free energy, BYO
validation object PEAR::Validate + zillions of others
try-catch in PHP5. no substitute for proper error
handling
“Web.config”- authentication auth, LiveUser, + others
ADO.Net- database abstraction db specific, dbx, PEAR DB, MDB,
ADODB
connection pooling, data-binding pconnect, SRM, only nice on client-side
MSXML expat, DOM, SimpleXML, XMLtree etc.,
2 SOAP, 5 XMLRPC
VisualStudio- IDE Komodo, Zend+ others, text editors
최태리
92
離
守
破
All of them (or equivalent) can be done
in PHP .
I have a choice on a solution—Zend’s
solution doesn’t bankrupt someone
else’s (or me!)
Most solutions are open source so I
don’t have be tied to someone else’s
release-upgrade cycle.
ASP.NET’s large framework violates
YAGNI—encourages o-o bigotry and
bad design.
二
최태리
93
Comparing PHP to
ASP.NET is like
comparing the Linux
kernel to the Windows
Platform.
GNU/Linux is the kernel
+ toolchain. That
toolchain is based on a
lot of small interacting
programs that can be
chained to create
complex behavior.
Windows does the
complicated approach.
we say Unix
has…
二
cohesion
vs coupling.
二
최태리
95
Frameworks vs.
Libraries
A good package is highly cohesive but
weakly coupled. This creates high
reusability.
Remember the J2EE taking 5x as long
to build? This is why. Too much
framework. When you use one part of
a package, you reuse them all.
This is why PEAR succeeds…
二
최태리
Some of these frameworks are great, but the
best are reused entirely (i.e. shrink-wrapped).
and Frameworks fail
Ariadne, Binary Cloud, Back-End, bBlog,
COWeb, Eocene, ezPublish, FastFrame,
FUDforum, ISMO, Komplete, LogiCreate
Application Server, Mambo, MetaL, Midgard,
more.groupware, NetUse, Nucleus,
phpGroupWare, Phrame, PostNuke,
PHPortal, phpWebSite, Rodin, SiteManager,
serendipity, STPHPLib, tikiwiki, TUTOS,
WordPress, XOOPS…
二
acyclic dependency
二
depend on stability
二
최태리
99
this covers acyclic
dependency
the more you
depend on a
package the more
stable it must be
(think client-
centric)
recall my devices
diagram…
Common
CameraSwitchable
Sensor
HVAC
二
최태리
100
Caveat: I’m going to use
that bad “construction”
analogy…
The Leaning Tower of
Pisa was built on a
shifting foundation. It’s
been leaning since
before it was finished.
Sounds like the what
happens when you you
try to extend someone
else’s framework.
二
최태리
101
This is a mapping of the
PEAR XML package
dependencies.
Notice how weakly
coupled they are.
Notice the packages
which are depended on.
What happens if the
API is changed.
What if you depend on
them? Better have your
Unit Tests!
XML_ParserXML_Util
XML_Beautifier
XML_CSSML
XML_Tree
XML_DTD
XML_fo2pdf
XML_HTMLSax
XML_image2sv
g
XML_NITF
XML_RSS
XML_SaxFilters
XML_Serializer
XML_sql2xml
XML_Statistics
XML_RDDL
二
102
roadmap
一
三
二
we now know the precepts
by which classes are made
and the parameters by whic
they are packaged into
classes…
離
守
破
103
roadmap
一
三
二
Before we go discuss
patterns it helps to define
what a pattern is!
If we skip this then we run
the danger of becoming a o-
o bigot or a grumpy old
programmer!
離
守
破
what are patterns?
二
최태리
105
Patterns come from a book on
architecture:
“Each pattern describes a problem
which occurs over and over again in our
environment and then describes the
core of the solution to that problem, in
such a way that you can use this solution
a million times over, without ever doing
it the same way twice.”
—Christopher Alexander, The Timeless
Way of Building, 1979
二
최태리
106
I don’t know architecture.
It makes me think of the software
construction metaphor
Let’s find some patterns closer to us…
Web Design!
whatever!
cookie
crumbs
solve deeply
hierarchical
navigation
tabs
solve lateral
navigation along a
few high level
elements
tabs
can use a million
times over, without
ever doing it the
same way twice
최태리
110
二definedbycontext
Let’s not be an o-o snob.
“tabs” and “cookie crumbs” are both
navigational patterns. Which one is
best to use is defined by context.
patterns are designed to
be linked together
二
phpunitpatterndensity
二
최태리
patterns
三
최태리
?baddesign
tale of the keyfob
三
115
mybane
Recall my poor design choice.
Now realize that a KeyFob has
four buttons. Each one is a
sensor.
Since I implemented the low
level stuff blindly and this
error occurred on the
presentation tier, the user sees
one keyfob as four devices.
One for each button!
Common
CameraSwitchable
Sensor
HVAC
三
최태리
How to make four
devices appear as one
?
三
최태리
!a pattern a day keeps the
pink slip away
composite
pattern
三
최태리
118
Common
Aggregate
A composite pattern
adds a subclass that
contains 1 or more
elements of the base
class.
Thus it appears to
clients as a single object,
even though it contains
more than one and have
to handle the objects
behind the scenes.
Common
CameraSwitchable
Sensor
HVAC
三
anotherexample三
최태리
?phpdoc
decoupled messaging
三
121
phpDocu
phpDocumentor is a PEAR
librar y/application that
tokenizes and parses your
PHP code for JavaDoc style
comments and uses them to
build hyperlinked API docs.
The problem is the parsers
(preg, tokenizer, c-based) and
the renderers (various HTML,
peardoc, XML, PDF) both
will need to vary in the PHP5
version.
The event messaging that
occurs can get quite complex.
三
최태리
How to make a more
robust messaging system
?
三
최태리
!allow one object to notify
many (run-time config)
observer
pattern
三
최태리
124
An observer registers
with the subject for
notification using the
attach() method passing
itself.
The subject notifies all
observers by calling its
notify() which iterates
over all observers and
calls its update() method
You can pass $this to
update() to allow
observer to use subject.
Subject
+ attach(&$observer)
+ detatch(&$observer)
# notify()
Observer
+ update()
ConcreteObserver
+ update()
ConcreteSubject
0..1*
三
최태리
?templates
working friendly with
designers
三
126
shutemplate
dotNet’s serverui
controls and auto page
state are great
but they’re template
systems
however PHP IDE !=
Visual Studio
離
守
破
三
127
1:<html>
2:<head>
3: <title>Example ASP.NET template</title>
4:</head>
5:<body>
6:<script language="VB" runat="server">
7:Sub Page_Load(sender As Object, e As EventArgs)
8: TheDate.Text = DateTime.Now
9:End Sub
10:</script>
11: <p>The current date is: <asp:Label id="TheDate"
runat="server" />.</p>
12:</body>
13:</html>
三
shu
how does this render in
an editor?
how does it separate
model-view-controller?
does MVC really matter
anyway?…
최태리
128
“With Web applications, nearly all the
engineering happens in the SQL
database and the interaction design,
which is embedded in the page flow
links. None of the extra power of Java is
useful…”
—Philip Greenspun
2003 September 20
三
129
hatemplate
PHP is itself a
templating language.
if all web developers are
PHP programmers this
makes the most sense
however
離
守
破
三
1:<html>
2:<head>
3: <title>Example ASP.NET template</title>
4:</head>
5:<body>
6:<script language="VB" runat="server">
7:Sub Page_Load(sender As Object, e As EventArgs)
8: TheDate.Text = DateTime.Now
9:End Sub
10:</script>
11: <p>The current date is: <asp:Label id="TheDate"
runat="server" />.</p>
12:</body>
13:</html>
1:<html>
2:<head>
3: <title>Example simple PHP version</title>
4:</head>
5:<body>
6: <p>The current date is: <?php echo date('l, F dS, y');
?>.</p>
7:</body>
8:</html>
三
shu ha
much smaller and easier to read!
how does a web designer deal with it?
최태리
!doing templates without
o-o
freeenergy
三
132
ritemplate
1. check for compiled
version.
2. if compiled then
include compiled
version
3. if not compiled then
call routine to compile
template
4. use XML!
離
守
破
三
133
1:<?php
2:tc::import('tips');
3:tc::import('tc.error');
4:
5:$template =& new tips();
6:$template->assign('date',date('l, F dS, y'));
7:$template->output('tipsexample.html');
8:?>Separate templates =
separate files
leverages shu
This is a smarttemplate
like system
leverages ha of php
use XML so the web
developer can work with
1:<tips:template>
2:<html>
3:<head>
4: <title>Example PHP template</title>
5:</head>
6:<body>
7: <p>The current date is: <tips:variable
name="date">Wednesday, October 21st, 2003</
tips:variable>.</p>
8:</body>
9:</html>
10:</tips:template>
三
최태리
}conclusion
三
최태리
?>questions
AndAnswers
三
http://homepage.mac.com/tychay/talks/oops

Contenu connexe

Tendances

PHP unserialization vulnerabilities: What are we missing?
PHP unserialization vulnerabilities: What are we missing?PHP unserialization vulnerabilities: What are we missing?
PHP unserialization vulnerabilities: What are we missing?Sam Thomas
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
ReactJS for Programmers
ReactJS for ProgrammersReactJS for Programmers
ReactJS for ProgrammersDavid Rodenas
 
Php7 HHVM and co
Php7 HHVM and coPhp7 HHVM and co
Php7 HHVM and coweltling
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormMichelangelo van Dam
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial之宇 趙
 
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010singingfish
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2Yi-Huan Chan
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Nikita Popov
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applicationschartjes
 
Some stuff about C++ and development
Some stuff about C++ and developmentSome stuff about C++ and development
Some stuff about C++ and developmentJon Jagger
 
Pyconie 2012
Pyconie 2012Pyconie 2012
Pyconie 2012Yaqi Zhao
 
Php 7 errors messages
Php 7 errors messagesPhp 7 errors messages
Php 7 errors messagesDamien Seguy
 
Php through the eyes of a hoster pbc10
Php through the eyes of a hoster pbc10Php through the eyes of a hoster pbc10
Php through the eyes of a hoster pbc10Combell NV
 
Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)Sharon Levy
 
TDD with PhpSpec - Lone Star PHP 2016
TDD with PhpSpec - Lone Star PHP 2016TDD with PhpSpec - Lone Star PHP 2016
TDD with PhpSpec - Lone Star PHP 2016CiaranMcNulty
 
Inside PyMongo - MongoNYC
Inside PyMongo - MongoNYCInside PyMongo - MongoNYC
Inside PyMongo - MongoNYCMike Dirolf
 
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testabilityPHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testabilityGiorgio Sironi
 

Tendances (20)

PHP unserialization vulnerabilities: What are we missing?
PHP unserialization vulnerabilities: What are we missing?PHP unserialization vulnerabilities: What are we missing?
PHP unserialization vulnerabilities: What are we missing?
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
ReactJS for Programmers
ReactJS for ProgrammersReactJS for Programmers
ReactJS for Programmers
 
Php7 HHVM and co
Php7 HHVM and coPhp7 HHVM and co
Php7 HHVM and co
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStorm
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial
 
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applications
 
Some stuff about C++ and development
Some stuff about C++ and developmentSome stuff about C++ and development
Some stuff about C++ and development
 
Pyconie 2012
Pyconie 2012Pyconie 2012
Pyconie 2012
 
Php 7 errors messages
Php 7 errors messagesPhp 7 errors messages
Php 7 errors messages
 
Php through the eyes of a hoster pbc10
Php through the eyes of a hoster pbc10Php through the eyes of a hoster pbc10
Php through the eyes of a hoster pbc10
 
Your code are my tests
Your code are my testsYour code are my tests
Your code are my tests
 
errors in php 7
errors in php 7errors in php 7
errors in php 7
 
Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)
 
TDD with PhpSpec - Lone Star PHP 2016
TDD with PhpSpec - Lone Star PHP 2016TDD with PhpSpec - Lone Star PHP 2016
TDD with PhpSpec - Lone Star PHP 2016
 
Inside PyMongo - MongoNYC
Inside PyMongo - MongoNYCInside PyMongo - MongoNYC
Inside PyMongo - MongoNYC
 
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testabilityPHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
 

En vedette

2013-08 10 evil things - Northeast PHP Conference Keynote
2013-08 10 evil things - Northeast PHP Conference Keynote2013-08 10 evil things - Northeast PHP Conference Keynote
2013-08 10 evil things - Northeast PHP Conference Keynoteterry chay
 
E.Ahmed Salama 2007
E.Ahmed Salama 2007E.Ahmed Salama 2007
E.Ahmed Salama 2007Ahmed Com
 
The last minute gift guide
The last minute gift guideThe last minute gift guide
The last minute gift guideAndy Desai
 
No Conversion Left Behind: Why National Brands are Shifting Dollars to Local
No Conversion Left Behind: Why National Brands are Shifting Dollars to LocalNo Conversion Left Behind: Why National Brands are Shifting Dollars to Local
No Conversion Left Behind: Why National Brands are Shifting Dollars to LocalBalihoo, Inc.
 
Canadian International Minerals Inc. (TSX-V: CIN) Finden bis zu 8,93% Cu, 0,1...
Canadian International Minerals Inc. (TSX-V: CIN) Finden bis zu 8,93% Cu, 0,1...Canadian International Minerals Inc. (TSX-V: CIN) Finden bis zu 8,93% Cu, 0,1...
Canadian International Minerals Inc. (TSX-V: CIN) Finden bis zu 8,93% Cu, 0,1...Canadian International Minerals Inc.
 
The role of leadership and employee well-being in digital business develpomen...
The role of leadership and employee well-being in digital business develpomen...The role of leadership and employee well-being in digital business develpomen...
The role of leadership and employee well-being in digital business develpomen...Riitta-Liisa Larjovuori
 
組織の文化をアジャイルに変えていくストーリー
組織の文化をアジャイルに変えていくストーリー組織の文化をアジャイルに変えていくストーリー
組織の文化をアジャイルに変えていくストーリーStefan Nüsperling
 
Feria universitaria agosto 2016
Feria universitaria agosto 2016Feria universitaria agosto 2016
Feria universitaria agosto 2016ramoncortes
 
Album de Partitura - Jorge Nobre
Album de Partitura - Jorge NobreAlbum de Partitura - Jorge Nobre
Album de Partitura - Jorge NobrePartitura de Banda
 

En vedette (12)

2013-08 10 evil things - Northeast PHP Conference Keynote
2013-08 10 evil things - Northeast PHP Conference Keynote2013-08 10 evil things - Northeast PHP Conference Keynote
2013-08 10 evil things - Northeast PHP Conference Keynote
 
E.Ahmed Salama 2007
E.Ahmed Salama 2007E.Ahmed Salama 2007
E.Ahmed Salama 2007
 
The last minute gift guide
The last minute gift guideThe last minute gift guide
The last minute gift guide
 
Memory Remembers...
Memory Remembers...Memory Remembers...
Memory Remembers...
 
No Conversion Left Behind: Why National Brands are Shifting Dollars to Local
No Conversion Left Behind: Why National Brands are Shifting Dollars to LocalNo Conversion Left Behind: Why National Brands are Shifting Dollars to Local
No Conversion Left Behind: Why National Brands are Shifting Dollars to Local
 
Canadian International Minerals Inc. (TSX-V: CIN) Finden bis zu 8,93% Cu, 0,1...
Canadian International Minerals Inc. (TSX-V: CIN) Finden bis zu 8,93% Cu, 0,1...Canadian International Minerals Inc. (TSX-V: CIN) Finden bis zu 8,93% Cu, 0,1...
Canadian International Minerals Inc. (TSX-V: CIN) Finden bis zu 8,93% Cu, 0,1...
 
BASEMAP
BASEMAPBASEMAP
BASEMAP
 
The role of leadership and employee well-being in digital business develpomen...
The role of leadership and employee well-being in digital business develpomen...The role of leadership and employee well-being in digital business develpomen...
The role of leadership and employee well-being in digital business develpomen...
 
組織の文化をアジャイルに変えていくストーリー
組織の文化をアジャイルに変えていくストーリー組織の文化をアジャイルに変えていくストーリー
組織の文化をアジャイルに変えていくストーリー
 
Feria universitaria agosto 2016
Feria universitaria agosto 2016Feria universitaria agosto 2016
Feria universitaria agosto 2016
 
Toccata for band
Toccata for bandToccata for band
Toccata for band
 
Album de Partitura - Jorge Nobre
Album de Partitura - Jorge NobreAlbum de Partitura - Jorge Nobre
Album de Partitura - Jorge Nobre
 

Similaire à The PHP Fear and Loathing Guide to Basic Object-Oriented Design

Professional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsProfessional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsMike Wilcox
 
Php interview questions
Php interview questionsPhp interview questions
Php interview questionssekar c
 
Php interview questions
Php interview questionsPhp interview questions
Php interview questionssubash01
 
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)Oleg Zinchenko
 
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоWebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоGeeksLab Odessa
 
Incredible Machine with Pipelines and Generators
Incredible Machine with Pipelines and GeneratorsIncredible Machine with Pipelines and Generators
Incredible Machine with Pipelines and Generatorsdantleech
 
The why and how of moving to php 5.4
The why and how of moving to php 5.4The why and how of moving to php 5.4
The why and how of moving to php 5.4Wim Godden
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - GreachHamletDRC
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHPPaul Houle
 
Yii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading toYii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading toAlexander Makarov
 
What To Expect From PHP7
What To Expect From PHP7What To Expect From PHP7
What To Expect From PHP7Codemotion
 

Similaire à The PHP Fear and Loathing Guide to Basic Object-Oriented Design (20)

Design patterns
Design patternsDesign patterns
Design patterns
 
Professional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsProfessional JavaScript: AntiPatterns
Professional JavaScript: AntiPatterns
 
More about PHP
More about PHPMore about PHP
More about PHP
 
Php interview questions
Php interview questionsPhp interview questions
Php interview questions
 
Current state-of-php
Current state-of-phpCurrent state-of-php
Current state-of-php
 
Php interview questions
Php interview questionsPhp interview questions
Php interview questions
 
Hardcore PHP
Hardcore PHPHardcore PHP
Hardcore PHP
 
php
phpphp
php
 
Dutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: DistilledDutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: Distilled
 
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)
 
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоWebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
 
Incredible Machine with Pipelines and Generators
Incredible Machine with Pipelines and GeneratorsIncredible Machine with Pipelines and Generators
Incredible Machine with Pipelines and Generators
 
The why and how of moving to php 5.4
The why and how of moving to php 5.4The why and how of moving to php 5.4
The why and how of moving to php 5.4
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - Greach
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHP
 
Yii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading toYii, frameworks and where PHP is heading to
Yii, frameworks and where PHP is heading to
 
Data herding
Data herdingData herding
Data herding
 
Data herding
Data herdingData herding
Data herding
 
What To Expect From PHP7
What To Expect From PHP7What To Expect From PHP7
What To Expect From PHP7
 
lab4_php
lab4_phplab4_php
lab4_php
 

Plus de terry chay

2019-03 PHP without PHP Architecture @ Confoo
2019-03 PHP without PHP Architecture @ Confoo2019-03 PHP without PHP Architecture @ Confoo
2019-03 PHP without PHP Architecture @ Confooterry chay
 
2019-03 Recomendation Engine @ Confoo
2019-03 Recomendation Engine @ Confoo2019-03 Recomendation Engine @ Confoo
2019-03 Recomendation Engine @ Confooterry chay
 
2019-02 The Recommendation Engine @ SunshinePHP
2019-02 The Recommendation Engine @ SunshinePHP2019-02 The Recommendation Engine @ SunshinePHP
2019-02 The Recommendation Engine @ SunshinePHPterry chay
 
2017-11 Recommendation Engine @ RaiseMe
2017-11 Recommendation Engine @ RaiseMe2017-11 Recommendation Engine @ RaiseMe
2017-11 Recommendation Engine @ RaiseMeterry chay
 
10 Evil(ish) Things and how they relate to Features Engineering at the WMF
10 Evil(ish) Things and how they relate to Features Engineering at the WMF10 Evil(ish) Things and how they relate to Features Engineering at the WMF
10 Evil(ish) Things and how they relate to Features Engineering at the WMFterry chay
 
2011 07 Tales of Virality—OSCON
2011 07 Tales of Virality—OSCON2011 07 Tales of Virality—OSCON
2011 07 Tales of Virality—OSCONterry chay
 
2011 07 Living without your Linemen—OSCON
2011 07 Living without your Linemen—OSCON2011 07 Living without your Linemen—OSCON
2011 07 Living without your Linemen—OSCONterry chay
 
PHP Without PHP—Automattic
PHP Without PHP—AutomatticPHP Without PHP—Automattic
PHP Without PHP—Automatticterry chay
 
Broken Jewel—Automattic
Broken Jewel—AutomatticBroken Jewel—Automattic
Broken Jewel—Automatticterry chay
 
Photo to Finished
Photo to FinishedPhoto to Finished
Photo to Finishedterry chay
 
Tales of Virality—Automattic
Tales of Virality—AutomatticTales of Virality—Automattic
Tales of Virality—Automatticterry chay
 
PHP Without PHP—Confoo
PHP Without PHP—ConfooPHP Without PHP—Confoo
PHP Without PHP—Confooterry chay
 
Chinese Proverbs—PHP|tek
Chinese Proverbs—PHP|tekChinese Proverbs—PHP|tek
Chinese Proverbs—PHP|tekterry chay
 
PHP Without PHP—IPC
PHP Without PHP—IPCPHP Without PHP—IPC
PHP Without PHP—IPCterry chay
 

Plus de terry chay (14)

2019-03 PHP without PHP Architecture @ Confoo
2019-03 PHP without PHP Architecture @ Confoo2019-03 PHP without PHP Architecture @ Confoo
2019-03 PHP without PHP Architecture @ Confoo
 
2019-03 Recomendation Engine @ Confoo
2019-03 Recomendation Engine @ Confoo2019-03 Recomendation Engine @ Confoo
2019-03 Recomendation Engine @ Confoo
 
2019-02 The Recommendation Engine @ SunshinePHP
2019-02 The Recommendation Engine @ SunshinePHP2019-02 The Recommendation Engine @ SunshinePHP
2019-02 The Recommendation Engine @ SunshinePHP
 
2017-11 Recommendation Engine @ RaiseMe
2017-11 Recommendation Engine @ RaiseMe2017-11 Recommendation Engine @ RaiseMe
2017-11 Recommendation Engine @ RaiseMe
 
10 Evil(ish) Things and how they relate to Features Engineering at the WMF
10 Evil(ish) Things and how they relate to Features Engineering at the WMF10 Evil(ish) Things and how they relate to Features Engineering at the WMF
10 Evil(ish) Things and how they relate to Features Engineering at the WMF
 
2011 07 Tales of Virality—OSCON
2011 07 Tales of Virality—OSCON2011 07 Tales of Virality—OSCON
2011 07 Tales of Virality—OSCON
 
2011 07 Living without your Linemen—OSCON
2011 07 Living without your Linemen—OSCON2011 07 Living without your Linemen—OSCON
2011 07 Living without your Linemen—OSCON
 
PHP Without PHP—Automattic
PHP Without PHP—AutomatticPHP Without PHP—Automattic
PHP Without PHP—Automattic
 
Broken Jewel—Automattic
Broken Jewel—AutomatticBroken Jewel—Automattic
Broken Jewel—Automattic
 
Photo to Finished
Photo to FinishedPhoto to Finished
Photo to Finished
 
Tales of Virality—Automattic
Tales of Virality—AutomatticTales of Virality—Automattic
Tales of Virality—Automattic
 
PHP Without PHP—Confoo
PHP Without PHP—ConfooPHP Without PHP—Confoo
PHP Without PHP—Confoo
 
Chinese Proverbs—PHP|tek
Chinese Proverbs—PHP|tekChinese Proverbs—PHP|tek
Chinese Proverbs—PHP|tek
 
PHP Without PHP—IPC
PHP Without PHP—IPCPHP Without PHP—IPC
PHP Without PHP—IPC
 

Dernier

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Dernier (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

The PHP Fear and Loathing Guide to Basic Object-Oriented Design

  • 1. 최태리 <?OOps! The PHP Fear and Loathing Guide to Basic Object-Oriented Design terry chay tagged 11 February 2009 engineering brownbag
  • 3. 최태리 “PhpPatterns is just that—a source of design patterns for PHP code. Will wonders never cease? I ran this by a Java programmer I know that I have drawn into the PHP world and her comment was, ‘I’d have never even thought to try patterns in PHP.’” —the FuzzyBlog phppatterns? {
  • 5. {
  • 7. 최태리 “ASP.NET applications are based on a robust Object Oriented Programming (OOP) paradigm rather than a scripting paradigm…The upside of ASP.NET’s support of OOP concepts [vs PHP] means that ASP.NET applications for the most part run better designed code, have clear separation of content, logic, and data and thus are generally easier to support over the long term.” —Migrating from PHP to ASP.NET dotFUD {
  • 8. 최태리 “Although being a young fellow myself, I have to hold my hand up and say I was educated in an era before design patterns were even invented. Yes it’s true, such a time did exist. In my day, it was called ‘Data Structures’.” —Alan Williamson, Editor-in-Chief November 2002 datastructures? {
  • 12. 離 12守 破 shuhold chinese character “house” and “law” to abide by; to defend the o-o bigot 7 habits: dependence {
  • 13. 離 13守 破 habreak chinese character “stone” + phonetic to break the grumpy old programmer 7 habits: independence {
  • 14. 離 14守 破 rileave chinese character “bird” + phonetic to leave; to depart the middle way 7 habits: interdependence {
  • 16. 16 myquiz 1:<?php 2:$tests[1] = new stdClass(); 3:$tests[2] = new stdClass(); 4: 5:foreach ($tests as $obj) { 6: $obj->foo = 'bar'; 7:} 8: 9:print_r($tests); 10:?> 1. 2. 3. tychay$ php test.php Array ( [1] => stdClass Object ( ) [2] => stdClass Object ( ) ) { 1. construct some generic PHP objects 2.interate over objects and assign them a value 3.dump the array of objects what do you think the output of this is and why? UNEXPECTED!
  • 17. 1:<?php 2:$tests[1] = new stdClass(); 3:$tests[2] = new stdClass(); 4: 5:foreach ($tests as $obj) { 6: $obj->foo = 'bar'; 7:} 8: 9:print_r($tests); 10:?> 17 myquiz remember that PHP4, objects are like everything else, so you need the &! Otherwise it will use a copy of the object. {
  • 18. 1:<?php 2:$tests[1] =& new stdClass(); 3:$tests[2] =& new stdClass(); 4: 5:foreach ($tests as $count=>$obj) { 6: $tests[$count]->foo = 'bar'; 7:} 8: 9:print_r($tests); 10:?> 18 myquiz tychay$ php answer1.php Array ( [1] => stdClass Object ( [foo] => bar ) [2] => stdClass Object ( [foo] => bar ) ) now we’ve fixed the problem! {
  • 19. 19 byreference { remember this is true also when: prints empty object! 1:<?php 2:class testClass 3:{ 4: function addObject(&$object) 5: { 6: $this->object = $object; 7: } 8:} 9:$testObject =& new testClass(); 10: 11:function addBar($object) 12:{ 13: $object->bar = 'this is set also'; 14:} 15:addBar($testObject); 16: 17:function getSameObject() 18:{ 19: global $testObject; 20: return $testObject; 21:} 22:$returnObject = getSameObject(); 23:$returnObject->same = 'this should be in object'; 24: 25:class testRefClass 26:{ 27: function testRefClass($objFromHell) 28: { 29: $objFromHell->something = 'something here'; 30: $objFromHell->addObject($this); 31: } 32:} 33:$tempObject = new testRefClass($testObject); 34:print_r($testObject); 1. passing parameters 3. constructor 2. functions that return objects
  • 20. 20 {1:<?php 2:class testClass 3:{ 4: function addObject(&$object) 5: { 6: $this->object = $object; 7: } 8:} 9:$testObject =& new testClass(); 10: 11:function addBar(&$object) 12:{ 13: $object->bar = 'this is set also'; 14:} 15:addBar($testObject); 16: 17:function &getSameObject() 18:{ 19: global $testObject; 20: return $testObject; 21:} 22:$returnObject =& getSameObject(); 23:$returnObject->same = 'this should be in object'; 24: 25:class testRefClass 26:{ 27: function testRefClass(&$objFromHell) 28: { 29: $objFromHell->something = 'something here'; 30: $objFromHell->addObject($this); 31: } 32:} 33:$tempObject =& new testRefClass($testObject); 34:print_r($testObject); byreference remember this is true also when: 1. passing parameters 3. constructor 2. functions that return objects PHP5 fixes this!
  • 21. 최태리 “…the new object model makes object oriented programming in PHP much more powerful and intuitive. No longer will you have to mess with cryptic & characters to get the job done. No longer will you have to worry about whether changes you made to the object inside the constructor will survive the dreaded new-operator behavior… php5 {
  • 22. 최태리 …No longer will you ever have to stay up until 2:00AM tracking elusive bugs!” —Zeev Suraski November 2002 php5 { Zeev’s2AMbug
  • 23. 최태리 23 staticproperties no static class variables in PHP. substitute with global variables static function variables (bad idea) {
  • 24. 24 globalstatic bind by reference in constructor (or elsewhere) 1:<?php 2:$GLOBALS['_statics']['testClass']['staticCount'] = 0; 3:class testClass 4:{ 5: var $staticCount; 6: function testClass() 7: { 8: $this->staticCount =& $GLOBALS['_statics'] ['testClass']['staticCount']; 9: } 10: function increment() 11: { 12: return ++$this->staticCount; 13: } 14:} 15:$test1 =& new testClass(); 16:$test2 =& new testClass(); 17:$test1->increment(); 18:$test2->increment(); 19:print_r($test1); 20:print_r($test2); 21:echo testClass::increment(); 22:?> tychay$ php test3.php testclass Object ( [staticCount] => 2 ) testclass Object ( [staticCount] => 2 ) 1 watch out though!
  • 25. 25 singleton one of the simplest patterns creates a single instance of a class (DB connection) lazy evaluation flexible 1:<?php 2:class testSingleton 3:{ 4: function &instance() 5: { 6: static $singleton; 7: if (!$singleton) { 8: $singleton =& new testSingleton(); 9: } 10: return $singleton; 11: } 12:} 13:$test1 =& testSingleton::instance(); 14:$test1->foo = bar; 15:$test2 =& testSingleton::instance(); 16:print_r($test2); 17:?> tychay$ php test4.php testsingleton Object ( ) watch out though!
  • 26. 26 singleton also note the liberal use of & 1:<?php 2:$GLOBALS['_statics']['testSingleton']['singleton'] = null; 3:class testSingleton 4:{ 5: function &instance() 6: { 7: if (is_null($GLOBALS['_statics']['testSingleton'] ['singleton'])) { 8: $GLOBALS['_statics']['testSingleton']['singleton'] =& new testSingleton(); 9: } 10: return $GLOBALS['_statics']['testSingleton'] ['singleton']; 11: } 12:} 13:$test1 =& testSingleton::instance(); 14:$test1->foo = bar; 15:$test2 =& testSingleton::instance(); 16:print_r($test2); 17:?> tychay$ php test5.php testsingleton Object ( [foo] => bar )
  • 27. 27 latebinding when a polymorphic call is resolved at run time (instead of compile time) oo-bigots advocate against (speed, strong typing) simple to do in PHP 1:<?php 2:class testLateBinding 3:{ 4: function callMeLate() 5: { 6: echo "I was calledn"; 7: $this->wasCalled = true; 8: } 9:} 10:$object1 =& new testLateBinding(); 11:$object2 =& new testLateBinding(); 12:$functionname = 'callMeLate'; 13: 14:$object1->{$functionname}(); 15: 16:call_user_func(array(&$object2,$functionname)); 17:print_r($object2); 18:?> tychay$ php test6.php I was called I was called testlatebinding Object ( [wasCalled] => 1 ) variable-variables call_user_func() PHP4.3+ note the &!. {
  • 29. 29 一 三 二 the why why do old metaphors fail? why use agile processes? methodology
  • 30. 30 一 三 二 the what what are the precepts of class rules what are the parameters of package design what are patterns? principles
  • 31. 31 一 三 二 the how how do patterns allow me to follow principles? how can patterns work around a design error? how do patterns apply to more than O-O? patterns
  • 35. 35 paradigm shift Code Complete is a “wanna be” Structure of Scientific Revolutions he uses “paradigm shift” Key premise: key metaphor of software should be construction 一
  • 36. 최태리 36 straw men The metaphor of construction is so central to the book, that the subtitle is: “A Practical Handbook of Software Construction.” Thus it needs to construct some straw men of failed metaphors in order to prove why we should adopt the “software as construction” paradigm. 一
  • 37. 최태리 37 一 1.software penmanship “writing code” causes expensive trial and error focused on originality > reuse “plan to throw one away” “it might have been state-of-the-art software-engineering practice in 1975”
  • 39. 최태리 39 growing a system “piece by piece” bad metaphor! fertilizing a system plan? thinning the detailed design? increasing code yields through effective land managment? harvesting code? rotating in a crop of assembler? Who ever believed this shit? 一
  • 40. 최태리 why Code Complete sucks software as construction 一
  • 41. The #1 reason why software is not construction 一 離 守 破
  • 42. Mythical Man Month This is a really famous essay from 1975. Does the year sound familiar? Yes, the Frederick Brooks is the same engineer treated with derision in Code Complete’s “Software as Writing” straw man. A lot of what he says was controversial, but one thing that was never denied was the man-month was a myth. What is a mythical man month? 一
  • 43. 43 man-month 一the premise is that men and months are interchangeable. This means that in order to reach a deadline I simply add people to the project.
  • 44. 최태리 44 man-month myth The problem: Adding people to a late project makes it later! first consider case where they’re everything is partition-able (man- month). then add constant time for training then add communication: n(n-1)/2 compare with unpartitionable (single man) 최채리
  • 45. 45 where? man-hour (măn’our') n. An industrial unit of production equal to the work one person can produce in an hour. Source: The American Heritage® Dictionary of the English Language, Fourth Edition fromconstruction! 一 programming!=labor
  • 46. 최태리 46 Mythical Man-Month referred to five times in Code Complete Including having the author give an endorsement on the back cover! irony php{con 최채리
  • 47. 최태리 47 최채리 “I’d be astonished if the open-source community has in total done as many man-years of computer security code reviews as we have done in the last two months” —Steven Lipner, Director of Security Assurance, Microsoft (April 8th, 2002) persistantmyth
  • 49. 최태리 49 “The final goal of any engineering activity is some type of documentation. When a design effort is complete, the design documentation is turned over to the manufacturing team. If the design documents truly represent a complete design, the manufacturing team can proceed to build the product.” —Jack Reeves, C++ Journal, 1992 一
  • 50. 최태리 50 “Two Irreparable Mistakes of thre Software Field: 1. The believe that the source code is a development product, not the design for a product.” —Michael Feathers, ObjectMentor The other problem with software as construction: the “design” phase finishes with working source code it’s based on the illusion that the source code is the product! 一
  • 51. 최태리 51 How did we buy this illusion? “It is cheaper and simpler to build the design and test it than do anything else. We do not care how many builds we do —they cost next to nothing…No other modern industry would tolerate a rework rate of over 100% in its manufacturing process” —Jack Reeves, C++ Journal, 1992 a software “build” means “manufacturing” phase is cheap. 一
  • 52. 최태리 離 守 破 A software developer is more akin to an artist than an assembly line worker… Throwing more people into the design mix can be counterproductive. —Jason Burkert “Software as engineering” accepts the reality of the mythical man-month! The source code is the design. 離 一
  • 53. 53 roadmap 一 三 二 now that we’ve talked discussed the paradigm or metaphor it is time to apply that to programming practice.
  • 54. 54 heavyweight 離 守 破 methodologies like ISO9001 violate “software as an engineering” by definition treat auxiliary documentation as the product
  • 55. 55 Note influence of construction metaphor BDUF: Big Design Up Front Big Bang Delivery waterfall SYSTEM SPECIFICATION REQUIREMENTS ANALYSIS ARCHITECTURAL DESIGN CODING AND DEBUGGING DETAILED DESIGN UNIT TESTING SYSTEM TESTING MAINTAINENCE from Code Complete, p.3 BDUF Big Bang 一
  • 56. 56 changing requirements constant release schedule not life-death “The reason why dynamic languages like Perl, Python, and PHP are so important … Unlike applications from the previous paradigm, web applications are not released in one to three year cycles. They are updated every day, sometimes every hour.” —Tim O’Reilly, 14 May 2003 websoftware 一
  • 57. 최태리 57 “The biggest mistake in ‘Build one to throw away’ concept is that it implicitly assumes the classical sequential or waterfall model of software construction.” —Frederick Brooks, Mythical Man- Month, 20th Aniv. 1995 moreirony 一
  • 59. 59 XP 離 守 破 ship early and often! one of many lightweight methodologies known as “agile” I’ll cover three features of XP. So you get an idea of what is considered an “agile” process 一
  • 61. designbytest recall the waterfall process. Look at where the unit testing is. recall the “software as engineering” metaphor: tests and builds are cheap. Why aren’t we leveraging it? We should be testing first and using testing as a design technique (the hard part of engineering), not a testing technique (easy part). SYSTEM SPECIFICATION REQUIREMENTS ANALYSIS ARCHITECTURAL DESIGN CODING AND DEBUGGING DETAILED DESIGN UNIT TESTING SYSTEM TESTING MAINTAINENCE 一
  • 62. 1:<?php 2:class MoneyBagTest extends TestCase { 3:    var $m12CHF; 4:    var $m14CHF; 5:    var $m28USD; 6:    var $mArray1; 7:    var $moneybag1; 8:    var $moneybag2; 9: function MoneyBagTest( $name = "MoneyBagTest" ) { 10: $this->TestCase( $name ); 11: } 12: 13: function setUp() { 14: $this->m12CHF = new Money( 12, "CHF" ); 15: $this->m14CHF = new Money( 14, "CHF" ); 16: } 17: function tearDown() { 18: $this->m12CHF = NULL; 19: $this->m14CHF = NULL; 20: $this->m28USD = NULL; 21: } 22: 23: function testSimpleAdd() { 24: $result = $this->m12CHF->add( $this->m14CHF ); 25: $this->assert(new Money(27,"CHF"), $result, "This should fail" ); 26: $this->assertEquals(new Money(27,"CHF"), $result, "This should fail" ); 27: } 28: 29: // insert more tests here: use "test..." for name of function. 30:} 62 code unit test first 1. Code the Unit Test First 2. Write minimum code until test works 3. Refactor (see later) 4. Rerun tests constructor a test 一 fixture
  • 63. keep the bar green to keep the code clean… 31: 32:$test = new MoneyBagTest(); 33:$testRunner = new TestRunner(); 34:$testRunner->run($test); 35:?>一
  • 65. 최태리 65 “The process of changing a software system in such a way that does not alter the external behavior of the code yet improves the internal structure” —Martin Fowler, Refactoring, 1999 the changes by themselves are tiny focus on simplicity (in design) not simplistic (implementation) refactorit! 一
  • 66. 최태리 66 do this continuously purpose of a module of code: function it performs allow change communicate prevents code rot in large projects my experience. refactorit! 一
  • 67. 3.You Aren’t Going to Need It (YAGNI) 一
  • 68. 최태리 68 Implement the simplest thing that could possibly work. Basically, if you need it, you can put it in later—it does not “save time.” Set aside your fears about tomorrow and concentrate on today. whatisYAGNI?
  • 69. 최태리 69 YAGNIperks keeps system smaller and easier to understand (form of refactoring) more likely to ship out the door if you focus on needs allows for system accretion recall how fast a web iteration vs. Big Bang
  • 70. 최태리 70 “A project done in Java will cost 5 times as much, take twice as long, and be harder to maintain than a project done in a scripting language such as PHP or Perl,” —Philip Greenspun 20 September 2003
  • 71. 최태리 71 lack of rigid O-O definitely a plus! recall the “Migrating from PHP to dotNET” document lack of framework definitely a plus! phpYAGNI
  • 72. 72 Well bad pattern for a loosly typed language such as PHP! Iterator Pattern: provide a way to access the element of an aggregate object sequentially without exposing its underlying representation badpattern Aggregate CreateIterator() ConcreteAggregate + CreateIterator() Iterator + First() + Next() + IsDone() + CurrentItem() ConcreteIterator return new ConcreteIterator(&$this)
  • 73. 1:<?php 2://A lot of code in here... 3:include(ECLIPSE_ROOT.'ArrayIterator.php'); 4: 5:$iterator =& new ArrayIterator(&$arrayToBeIterated); 6:while ($element=$iterator->getCurrent()) { 7: echo $iterator->key().' => '.$element; 8: $iterator->next(); 9:} 10:$iterator->reset(); 11: 12:?> iterator# phpdefault You can vary what is being iterated over (ignoring constructor) YAGNI! If you must have an iterator there is SPL in PHP5! 1:<?php 2:foreach($arrayToBeIterated as $key=>$value) { 3: echo $key.' => '.$value; 4:} 5:?>
  • 74. 최태리 74 pair programming the planning game/user-stories code reviews acceptance tests on-site customer other processes: SCRUM, Crystal, etc. code ownership XPnotcovered 一
  • 75. 75 roadmap 一 三 二 the metaphor/paradigm for building software is engineering we now know agility: the lightweight processes that avoid the waterfall process 離 守 破
  • 78. 78 roadmap 一 三 二 1. What precepts for class design? 2. Grouping classes into packages using what parameters? 3. What are patterns?
  • 80. 최태리 80 single responsibility A class should have only one reason to change. Each responsibility is an axis of change. more than one responsibility = coupling 二
  • 81. 81 violation PEAR is great. Except… This is the PEAR Error constructor. Note that this method causes the object to violate SRP! 2:function PEAR_Error($message = 'unknown error', $code = null, 3: $mode = null, $options = null, $userinfo = null) 4:{ 5: if ($mode === null) { 6: $mode = PEAR_ERROR_RETURN; 7: } 8: $this->message = $message; 9: $this->code = $code; 10: $this->mode = $mode; 11: $this->userinfo = $userinfo; 12: if (function_exists("debug_backtrace")) { 13: $this->backtrace = debug_backtrace(); 14: } 15: if ($mode & PEAR_ERROR_CALLBACK) { 16: $this->level = E_USER_NOTICE; 17: $this->callback = $options; 18: } else { 19: if ($options === null) { 20: $options = E_USER_NOTICE; 21: } 22: $this->level = $options; 23: $this->callback = null; 24: } 25: if ($this->mode & PEAR_ERROR_PRINT) { 26: if (is_null($options) || is_int($options)) { 27: $format = "%s"; 28: } else { 29: $format = $options; 30: } 31: printf($format, $this->getMessage()); 32: } 33: if ($this->mode & PEAR_ERROR_TRIGGER) { 34: trigger_error($this->getMessage(), $this->level); 35: }
  • 82. 82 and the violation goes on callbacks die return mode print trigger_error exception 36: if ($this->mode & PEAR_ERROR_DIE) { 37: $msg = $this->getMessage(); 38: if (is_null($options) || is_int($options)) { 39: $format = "%s"; 40: if (substr($msg, -1) != "n") { 41: $msg .= "n"; 42: } 43: } else { 44: $format = $options; 45: } 46: die(sprintf($format, $msg)); 47: } 48: if ($this->mode & PEAR_ERROR_CALLBACK) { 49: if (is_string($this->callback) && strlen($this- >callback)) { 50: call_user_func($this->callback, $this); 51: } elseif (is_array($this->callback) && 52: sizeof($this->callback) == 2 && 53: is_object($this->callback[0]) && 54: is_string($this->callback[1]) && 55: strlen($this->callback[1])) { 56: @call_user_func($this->callback, $this); 57: } 58: } 59: if (PEAR_ZE2 && $this->mode & PEAR_ERROR_EXCEPTION) { 60: eval('throw $this;'); 61: } 62:} 63:?>
  • 83. 최태리 83 Liskov substitution Subtypes must be substitutable for their base types. not an “is-a” relationship validation of LSP occurs in the client! (Design By Contract). 二
  • 84. 최태리 84 Design By Contract validates Liskov Substituion. What validates Design by contract? Unit Tests! Remember them? They determine define it. In fact, PEAR PHPUnit can document a Design By Contract by reading the unit test (aka TestDox). 1:<?php 2:require_once 'PHPUnit/Framework/TestCase.php'; 3: 4:class FooTest extends PHPUnit_Framework_TestCase 5:{ 6:    public function testIsASingleton() {} 7:    public function testAReallyLongNameIsAGoodThing() {} 8:} 9:?> Foo - Is a singleton - A really long name is a good thing
  • 85. I violate LSP Look at how naïve this diagram is. It’s the most obvious one but focuses on the physical objects instead of their behaviors. The objects are defined in terms of the clients. Think about an image coming back from the camera, or trying to set a dimmer to a light switch that turns on or off only. Unfortunately I had to live with it for reasons later. Common CameraSwitchable Sensor HVAC
  • 88. 최태리 88 “A few days ago, I was at Microsoft, and they held an amazing conference…After this conference, I was kinda depressed about the future of PHP and Apache… The ASP.NET stuff is just perfect. Fast, Secure and Robust. ASP.NET technology is better than PHP technology.” —Sterling Hughes 5 June 2003 二
  • 89. 최태리 Common language Runtime access to COM+ server ui controls “code behind” (.cs) server-side object model event-driven handlers automatic page state (ViewState) built-in Validation object try-catch 二
  • 90. 최태리 built-in authentication (Web.config) built-in database abstraction (ADO.NET) + connection pooling + data binding built-in MSXML: DOM, XSLT, SAX + Web Services auto targeting of HTML IDE (Visual Studio .NET) built-in output caching I see what Sterling is saying! However… 二
  • 91. 최태리 Common Language Runtime 2nd class, compiling cache, Pint (Parrot) access to COM+ access to COM, Java, C/C++ extensions, mono, command line server ui controls,“ViewState”- saving form state PEAR::HTML_* (Form, Menu, Page…) + others + BYO “code behind” smarty, fasttemplate, flexy, it, phplib, + others, BYO server-side object model session, __sleep(), __wakeup(), SRM event driven handlers bad idea, fusebox + zillions of others, s9y, BYO output-caching zend accelerator, Cache, Cache_Lite, free energy, BYO validation object PEAR::Validate + zillions of others try-catch in PHP5. no substitute for proper error handling “Web.config”- authentication auth, LiveUser, + others ADO.Net- database abstraction db specific, dbx, PEAR DB, MDB, ADODB connection pooling, data-binding pconnect, SRM, only nice on client-side MSXML expat, DOM, SimpleXML, XMLtree etc., 2 SOAP, 5 XMLRPC VisualStudio- IDE Komodo, Zend+ others, text editors
  • 92. 최태리 92 離 守 破 All of them (or equivalent) can be done in PHP . I have a choice on a solution—Zend’s solution doesn’t bankrupt someone else’s (or me!) Most solutions are open source so I don’t have be tied to someone else’s release-upgrade cycle. ASP.NET’s large framework violates YAGNI—encourages o-o bigotry and bad design. 二
  • 93. 최태리 93 Comparing PHP to ASP.NET is like comparing the Linux kernel to the Windows Platform. GNU/Linux is the kernel + toolchain. That toolchain is based on a lot of small interacting programs that can be chained to create complex behavior. Windows does the complicated approach. we say Unix has… 二
  • 95. 최태리 95 Frameworks vs. Libraries A good package is highly cohesive but weakly coupled. This creates high reusability. Remember the J2EE taking 5x as long to build? This is why. Too much framework. When you use one part of a package, you reuse them all. This is why PEAR succeeds… 二
  • 96. 최태리 Some of these frameworks are great, but the best are reused entirely (i.e. shrink-wrapped). and Frameworks fail Ariadne, Binary Cloud, Back-End, bBlog, COWeb, Eocene, ezPublish, FastFrame, FUDforum, ISMO, Komplete, LogiCreate Application Server, Mambo, MetaL, Midgard, more.groupware, NetUse, Nucleus, phpGroupWare, Phrame, PostNuke, PHPortal, phpWebSite, Rodin, SiteManager, serendipity, STPHPLib, tikiwiki, TUTOS, WordPress, XOOPS… 二
  • 99. 최태리 99 this covers acyclic dependency the more you depend on a package the more stable it must be (think client- centric) recall my devices diagram… Common CameraSwitchable Sensor HVAC 二
  • 100. 최태리 100 Caveat: I’m going to use that bad “construction” analogy… The Leaning Tower of Pisa was built on a shifting foundation. It’s been leaning since before it was finished. Sounds like the what happens when you you try to extend someone else’s framework. 二
  • 101. 최태리 101 This is a mapping of the PEAR XML package dependencies. Notice how weakly coupled they are. Notice the packages which are depended on. What happens if the API is changed. What if you depend on them? Better have your Unit Tests! XML_ParserXML_Util XML_Beautifier XML_CSSML XML_Tree XML_DTD XML_fo2pdf XML_HTMLSax XML_image2sv g XML_NITF XML_RSS XML_SaxFilters XML_Serializer XML_sql2xml XML_Statistics XML_RDDL 二
  • 102. 102 roadmap 一 三 二 we now know the precepts by which classes are made and the parameters by whic they are packaged into classes… 離 守 破
  • 103. 103 roadmap 一 三 二 Before we go discuss patterns it helps to define what a pattern is! If we skip this then we run the danger of becoming a o- o bigot or a grumpy old programmer! 離 守 破
  • 105. 최태리 105 Patterns come from a book on architecture: “Each pattern describes a problem which occurs over and over again in our environment and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.” —Christopher Alexander, The Timeless Way of Building, 1979 二
  • 106. 최태리 106 I don’t know architecture. It makes me think of the software construction metaphor Let’s find some patterns closer to us… Web Design! whatever!
  • 108. tabs solve lateral navigation along a few high level elements
  • 109. tabs can use a million times over, without ever doing it the same way twice
  • 110. 최태리 110 二definedbycontext Let’s not be an o-o snob. “tabs” and “cookie crumbs” are both navigational patterns. Which one is best to use is defined by context.
  • 111. patterns are designed to be linked together 二
  • 115. 115 mybane Recall my poor design choice. Now realize that a KeyFob has four buttons. Each one is a sensor. Since I implemented the low level stuff blindly and this error occurred on the presentation tier, the user sees one keyfob as four devices. One for each button! Common CameraSwitchable Sensor HVAC 三
  • 116. 최태리 How to make four devices appear as one ? 三
  • 117. 최태리 !a pattern a day keeps the pink slip away composite pattern 三
  • 118. 최태리 118 Common Aggregate A composite pattern adds a subclass that contains 1 or more elements of the base class. Thus it appears to clients as a single object, even though it contains more than one and have to handle the objects behind the scenes. Common CameraSwitchable Sensor HVAC 三
  • 121. 121 phpDocu phpDocumentor is a PEAR librar y/application that tokenizes and parses your PHP code for JavaDoc style comments and uses them to build hyperlinked API docs. The problem is the parsers (preg, tokenizer, c-based) and the renderers (various HTML, peardoc, XML, PDF) both will need to vary in the PHP5 version. The event messaging that occurs can get quite complex. 三
  • 122. 최태리 How to make a more robust messaging system ? 三
  • 123. 최태리 !allow one object to notify many (run-time config) observer pattern 三
  • 124. 최태리 124 An observer registers with the subject for notification using the attach() method passing itself. The subject notifies all observers by calling its notify() which iterates over all observers and calls its update() method You can pass $this to update() to allow observer to use subject. Subject + attach(&$observer) + detatch(&$observer) # notify() Observer + update() ConcreteObserver + update() ConcreteSubject 0..1* 三
  • 126. 126 shutemplate dotNet’s serverui controls and auto page state are great but they’re template systems however PHP IDE != Visual Studio 離 守 破 三
  • 127. 127 1:<html> 2:<head> 3: <title>Example ASP.NET template</title> 4:</head> 5:<body> 6:<script language="VB" runat="server"> 7:Sub Page_Load(sender As Object, e As EventArgs) 8: TheDate.Text = DateTime.Now 9:End Sub 10:</script> 11: <p>The current date is: <asp:Label id="TheDate" runat="server" />.</p> 12:</body> 13:</html> 三 shu how does this render in an editor? how does it separate model-view-controller? does MVC really matter anyway?…
  • 128. 최태리 128 “With Web applications, nearly all the engineering happens in the SQL database and the interaction design, which is embedded in the page flow links. None of the extra power of Java is useful…” —Philip Greenspun 2003 September 20 三
  • 129. 129 hatemplate PHP is itself a templating language. if all web developers are PHP programmers this makes the most sense however 離 守 破 三
  • 130. 1:<html> 2:<head> 3: <title>Example ASP.NET template</title> 4:</head> 5:<body> 6:<script language="VB" runat="server"> 7:Sub Page_Load(sender As Object, e As EventArgs) 8: TheDate.Text = DateTime.Now 9:End Sub 10:</script> 11: <p>The current date is: <asp:Label id="TheDate" runat="server" />.</p> 12:</body> 13:</html> 1:<html> 2:<head> 3: <title>Example simple PHP version</title> 4:</head> 5:<body> 6: <p>The current date is: <?php echo date('l, F dS, y'); ?>.</p> 7:</body> 8:</html> 三 shu ha much smaller and easier to read! how does a web designer deal with it?
  • 132. 132 ritemplate 1. check for compiled version. 2. if compiled then include compiled version 3. if not compiled then call routine to compile template 4. use XML! 離 守 破 三
  • 133. 133 1:<?php 2:tc::import('tips'); 3:tc::import('tc.error'); 4: 5:$template =& new tips(); 6:$template->assign('date',date('l, F dS, y')); 7:$template->output('tipsexample.html'); 8:?>Separate templates = separate files leverages shu This is a smarttemplate like system leverages ha of php use XML so the web developer can work with 1:<tips:template> 2:<html> 3:<head> 4: <title>Example PHP template</title> 5:</head> 6:<body> 7: <p>The current date is: <tips:variable name="date">Wednesday, October 21st, 2003</ tips:variable>.</p> 8:</body> 9:</html> 10:</tips:template> 三