SlideShare a Scribd company logo
1 of 23
Download to read offline
thomas@gasc.fr @methylbro 
Manage your objects 
Tuesday 9th of September 2014 Montpellier
/* ask name */ 
fwrite(STDOUT, "enter your name :"); 
$name = trim(fgets(STDIN, 255)); 
/* ask birthday */ 
fwrite(STDOUT, "enter your birthday (yyyy/mm/dd) :"); 
$birthday = new DateTime(trim(fgets(STDIN, 255))); 
/* calculate the age */ 
$age = $birthday->diff(new DateTime()); 
Case study 
/* display the result */ 
fwrite( 
STDOUT, 
sprintf(PHP_EOL."hello %s you've got %s years old.", $name, $age->y) 
);
Oriented Object Programing
Reader 
+ handle 
+ length 
+ read(msg) 
Writer 
+ handle 
+ write(msg) 
AgeCalculator 
+ execute() 
Application Class Diagram
// src/Reader.php 
class Reader 
{ 
private $handle = STDIN; 
private $length = 255; 
private $writer; 
public function __construct() 
{ 
$this->writer = new Writer(); 
} 
public function read($msg) 
{ 
$this->writer->write($msg); 
return trim(fgets($this->handle, $this->length)); 
} 
}
// src/AgeCalculator.php 
class AgeCalculator 
{ 
private $reader, $writer; 
public function __construct() 
{ 
$this->reader = new Reader(); 
$this->writer = new Writer(); 
} 
public function execute() 
{ 
$name = $this->reader->read('your name:'); 
$birthday = new DateTime( 
$this->reader->read('your birthday (yyyy/mm/dd):') 
); 
$age = $birthday->diff(new DateTime('now')); 
$this->writer->write( 
sprintf("hello %s you've got %s years old.", $name, $age->y) 
); 
} 
}
// age-calculator.php 
$ageCalculator = new AgeCalculator(); 
$ageCalculator->execute(); 
$ php age-calculator.php 
your name: thomas 
your birthday (yyyy/mm/dd): 1994/01/01 
hello thomas you've got 20 years old. 
Running the application
Dependency Injection 
Pass a dependency to a dependent object
// src/Reader.php 
class Reader 
{ 
private $handle, $length, $writer; 
public function __construct(Writer $writer = null, $handle = STDIN, $length = 255) 
{ 
$this->handle = $handle; 
$this->length = $length; 
$this->writer = $writer; 
} 
public function read($msg) 
{ 
if ($this->writer) { 
$this->writer->write($msg); 
} 
return trim(fgets($this->handle, $this->length)); 
} 
}
// age-calculator.php 
$writer = new Writer(); 
$reader = new Reader($writer); 
$ageCalculator = new AgeCalculator($writer, $reader); 
$ageCalculator->execute();
// age-calculator.php 
$writer = new Writer(fopen('result', 'w+')); 
$reader = new Reader(null, fopen('data', 'r')); 
$ageCalculator = new AgeCalculator($writer, $reader); 
$ageCalculator->execute();
Using a Dependency Injection 
Framework
// config/cli.php 
return [ 
'Writer' => DIobject()->constructor(), 
'Reader' => DIobject() 
->constructor(DIlink('Writer')), 
'AgeCalculator' => DIobject() 
->constructor(DIlink('Writer'), DIlink('Reader')), 
];
// age-calculator.php 
require 'vendor/autoload.php'; 
$builder = new DIContainerBuilder(); 
$builder->addDefinitions('config/cli.php'); 
$container = $builder->build(); 
$ageCalculator = $container->get('AgeCalculator'); 
$ageCalculator->execute();
// config/file.php 
return [ 
'handle.result' => fopen('result', 'w+'), 
'handle.data' => fopen('data', 'r'), 
'Writer' => DIobject() 
->constructor(DIlink('handle.result')), 
'Reader' => DIobject() 
->constructor(null, DIlink('handle.data')), 
'AgeCalculator' => DIobject() 
->constructor(DIlink('Writer'), DIlink('Reader')), 
];
Factory Design Pattern 
Deals with the problem of creating objects without 
specifying the exact class of object that will be created
<< abstract >> 
Factory 
ReaderFactory WriterFactory 
+ create() 
creates creates 
+ write(msg) 
FileWriter 
uses 
Reader Writer 
<< interface >> 
ReaderInterface 
+ setWriter(writer) 
+ read(msg) 
FileReader 
<< interface >> 
WriterInterface 
extends extends 
extends extends 
extends extends
// src/FileWriter.php 
class FileWriter extends Writer 
{ 
public function __construct($filename, $length = 255) 
{ 
$this->handle = fopen($filename, 'w+'); 
$this->length = $length; 
} 
} 
// src/Filereader.php 
class FileReader extends Reader 
{ 
public function __construct($filename, $length = 255) 
{ 
$this->handle = fopen($filename, 'r'); 
$this->length = $length; 
} 
}
// src/Factory.php 
abstract class Factory 
{ 
private $class; 
public function __construct($class) 
{ 
$this->class = new ReflectionClass($class); 
} 
protected abstract function configure(ReflectionClass $class); 
public function create($args = array()) 
{ 
return $this->class->newInstanceArgs($args); 
} 
}
// config/file.php 
return [ 
'writer.class' => 'FileWriter', 
'reader.class' => 'FileReader', 
'data.file' => 'data', 
'result.file' => 'result', 
'WriterFactory' => DIobject()->constructor(DIlink('writer.class')), 
'ReaderFactory' => DIobject() 
->constructor( 
DIlink('reader.class')), 
DIfactory(function ($container) { 
return $container 
->get('WriterFactory') 
->create(array($container->get('result.file'))) 
; 
}), 
DIfactory(function ($container) { 
return $container 
->get('ReaderFactory') 
->create(array($container->get('data.file'))) 
; 
} 
), 
'AgeCalculator' => DIobject()->constructor(DIlink('writer'), DIlink('reader')), 
];
Singleton Design Pattern 
Restricts the instantiation of a class to one object
Singleton
Questions ? 
Full examples 
https://github.com/methylbro/human-talk-manage-objects-examples

More Related Content

What's hot

Anonymous classes
Anonymous classesAnonymous classes
Anonymous classesDarkmira
 
Perl6 operators and metaoperators
Perl6   operators and metaoperatorsPerl6   operators and metaoperators
Perl6 operators and metaoperatorsSimon Proctor
 
Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12Jakub Zalas
 
Revisiting SOLID Principles
Revisiting  SOLID Principles Revisiting  SOLID Principles
Revisiting SOLID Principles Anis Ahmad
 
Phing - PHP Conference 2015
Phing -  PHP Conference 2015Phing -  PHP Conference 2015
Phing - PHP Conference 2015Matheus Marabesi
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding HorrorsMark Baker
 
Pim Elshoff "Technically DDD"
Pim Elshoff "Technically DDD"Pim Elshoff "Technically DDD"
Pim Elshoff "Technically DDD"Fwdays
 
TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !Matheus Marabesi
 
Jquery optimization-tips
Jquery optimization-tipsJquery optimization-tips
Jquery optimization-tipsanubavam-techkt
 
The Truth About Lambdas in PHP
The Truth About Lambdas in PHPThe Truth About Lambdas in PHP
The Truth About Lambdas in PHPSharon Levy
 
PHP for Adults: Clean Code and Object Calisthenics
PHP for Adults: Clean Code and Object CalisthenicsPHP for Adults: Clean Code and Object Calisthenics
PHP for Adults: Clean Code and Object CalisthenicsGuilherme Blanco
 
Uncovering Iterators
Uncovering IteratorsUncovering Iterators
Uncovering Iteratorssdevalk
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixturesBill Chang
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix itRafael Dohms
 
File upload for the 21st century
File upload for the 21st centuryFile upload for the 21st century
File upload for the 21st centuryJiří Pudil
 
Path::Tiny
Path::TinyPath::Tiny
Path::Tinywaniji
 
Object Calisthenics Applied to PHP
Object Calisthenics Applied to PHPObject Calisthenics Applied to PHP
Object Calisthenics Applied to PHPGuilherme Blanco
 
Advanced modulinos trial
Advanced modulinos trialAdvanced modulinos trial
Advanced modulinos trialbrian d foy
 

What's hot (20)

Anonymous classes
Anonymous classesAnonymous classes
Anonymous classes
 
Perl6 operators and metaoperators
Perl6   operators and metaoperatorsPerl6   operators and metaoperators
Perl6 operators and metaoperators
 
Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12
 
Revisiting SOLID Principles
Revisiting  SOLID Principles Revisiting  SOLID Principles
Revisiting SOLID Principles
 
Phing - PHP Conference 2015
Phing -  PHP Conference 2015Phing -  PHP Conference 2015
Phing - PHP Conference 2015
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding Horrors
 
Pim Elshoff "Technically DDD"
Pim Elshoff "Technically DDD"Pim Elshoff "Technically DDD"
Pim Elshoff "Technically DDD"
 
TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !
 
Php variables
Php variablesPhp variables
Php variables
 
Migrare da symfony 1 a Symfony2
 Migrare da symfony 1 a Symfony2  Migrare da symfony 1 a Symfony2
Migrare da symfony 1 a Symfony2
 
Jquery optimization-tips
Jquery optimization-tipsJquery optimization-tips
Jquery optimization-tips
 
The Truth About Lambdas in PHP
The Truth About Lambdas in PHPThe Truth About Lambdas in PHP
The Truth About Lambdas in PHP
 
PHP for Adults: Clean Code and Object Calisthenics
PHP for Adults: Clean Code and Object CalisthenicsPHP for Adults: Clean Code and Object Calisthenics
PHP for Adults: Clean Code and Object Calisthenics
 
Uncovering Iterators
Uncovering IteratorsUncovering Iterators
Uncovering Iterators
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
 
File upload for the 21st century
File upload for the 21st centuryFile upload for the 21st century
File upload for the 21st century
 
Path::Tiny
Path::TinyPath::Tiny
Path::Tiny
 
Object Calisthenics Applied to PHP
Object Calisthenics Applied to PHPObject Calisthenics Applied to PHP
Object Calisthenics Applied to PHP
 
Advanced modulinos trial
Advanced modulinos trialAdvanced modulinos trial
Advanced modulinos trial
 

Viewers also liked

Conception et programmation orientées individus
Conception et programmation orientées individusConception et programmation orientées individus
Conception et programmation orientées individusThomas Gasc
 
Business 202 - How to Take Your Business to the Next Level
Business 202 - How to Take Your Business to the Next LevelBusiness 202 - How to Take Your Business to the Next Level
Business 202 - How to Take Your Business to the Next LevelBlatt Financial Group, LLC.
 
Proiect tic a_2b_horodinca_margareta
Proiect tic a_2b_horodinca_margaretaProiect tic a_2b_horodinca_margareta
Proiect tic a_2b_horodinca_margaretamarge8
 
110724 laporan pendahuluan
110724 laporan pendahuluan110724 laporan pendahuluan
110724 laporan pendahuluanyudiarimbawa
 
Blatt Financial Group hosts 138th Annual Kentucky Derby Party!
Blatt Financial Group hosts 138th Annual Kentucky Derby Party!Blatt Financial Group hosts 138th Annual Kentucky Derby Party!
Blatt Financial Group hosts 138th Annual Kentucky Derby Party!Blatt Financial Group, LLC.
 
Y'a pas d'avancement, pas de grimaces !
Y'a pas d'avancement, pas de grimaces !Y'a pas d'avancement, pas de grimaces !
Y'a pas d'avancement, pas de grimaces !Thomas Gasc
 
Pourquoi WordPress n’est pas un CMS
Pourquoi WordPress n’est pas un CMSPourquoi WordPress n’est pas un CMS
Pourquoi WordPress n’est pas un CMSThomas Gasc
 
[별천지 세미나] HTML5 is Ready: Fastbook 기술적 분석
[별천지 세미나] HTML5 is Ready: Fastbook 기술적 분석[별천지 세미나] HTML5 is Ready: Fastbook 기술적 분석
[별천지 세미나] HTML5 is Ready: Fastbook 기술적 분석Junki Kim
 
2012 최신회사 제안서
2012 최신회사 제안서2012 최신회사 제안서
2012 최신회사 제안서동헌 이
 
Volvo Group - Design Management
Volvo Group - Design ManagementVolvo Group - Design Management
Volvo Group - Design Managementlipinartur
 
April Client Appreciation Event: Brunch and Polo
April Client Appreciation Event: Brunch and PoloApril Client Appreciation Event: Brunch and Polo
April Client Appreciation Event: Brunch and PoloBlatt Financial Group, LLC.
 
E. pendekatan, metodologi dan program kerja1
E. pendekatan, metodologi dan program kerja1E. pendekatan, metodologi dan program kerja1
E. pendekatan, metodologi dan program kerja1yudiarimbawa
 
20110311 ecran geantv3
20110311   ecran geantv320110311   ecran geantv3
20110311 ecran geantv3jylaw
 

Viewers also liked (16)

Power point for web
Power point for webPower point for web
Power point for web
 
Conception et programmation orientées individus
Conception et programmation orientées individusConception et programmation orientées individus
Conception et programmation orientées individus
 
Business 202 - How to Take Your Business to the Next Level
Business 202 - How to Take Your Business to the Next LevelBusiness 202 - How to Take Your Business to the Next Level
Business 202 - How to Take Your Business to the Next Level
 
Proiect tic a_2b_horodinca_margareta
Proiect tic a_2b_horodinca_margaretaProiect tic a_2b_horodinca_margareta
Proiect tic a_2b_horodinca_margareta
 
March Client Appreciation Event
March Client Appreciation EventMarch Client Appreciation Event
March Client Appreciation Event
 
110724 laporan pendahuluan
110724 laporan pendahuluan110724 laporan pendahuluan
110724 laporan pendahuluan
 
Blatt Financial Group hosts 138th Annual Kentucky Derby Party!
Blatt Financial Group hosts 138th Annual Kentucky Derby Party!Blatt Financial Group hosts 138th Annual Kentucky Derby Party!
Blatt Financial Group hosts 138th Annual Kentucky Derby Party!
 
Y'a pas d'avancement, pas de grimaces !
Y'a pas d'avancement, pas de grimaces !Y'a pas d'avancement, pas de grimaces !
Y'a pas d'avancement, pas de grimaces !
 
Pourquoi WordPress n’est pas un CMS
Pourquoi WordPress n’est pas un CMSPourquoi WordPress n’est pas un CMS
Pourquoi WordPress n’est pas un CMS
 
[별천지 세미나] HTML5 is Ready: Fastbook 기술적 분석
[별천지 세미나] HTML5 is Ready: Fastbook 기술적 분석[별천지 세미나] HTML5 is Ready: Fastbook 기술적 분석
[별천지 세미나] HTML5 is Ready: Fastbook 기술적 분석
 
2012 최신회사 제안서
2012 최신회사 제안서2012 최신회사 제안서
2012 최신회사 제안서
 
Volvo Group - Design Management
Volvo Group - Design ManagementVolvo Group - Design Management
Volvo Group - Design Management
 
April Client Appreciation Event: Brunch and Polo
April Client Appreciation Event: Brunch and PoloApril Client Appreciation Event: Brunch and Polo
April Client Appreciation Event: Brunch and Polo
 
PHP Quiz
PHP QuizPHP Quiz
PHP Quiz
 
E. pendekatan, metodologi dan program kerja1
E. pendekatan, metodologi dan program kerja1E. pendekatan, metodologi dan program kerja1
E. pendekatan, metodologi dan program kerja1
 
20110311 ecran geantv3
20110311   ecran geantv320110311   ecran geantv3
20110311 ecran geantv3
 

Similar to Gérer vos objets

How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleHugo Hamon
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Fabien Potencier
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hackingJeroen van Dijk
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)James Titcumb
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)James Titcumb
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmersAlexander Varwijk
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosDivante
 
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof MenżykPROIDEA
 
Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Stephan Schmidt
 
How kris-writes-symfony-apps-london
How kris-writes-symfony-apps-londonHow kris-writes-symfony-apps-london
How kris-writes-symfony-apps-londonKris Wallsmith
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsMichael Peacock
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your CodeAbbas Ali
 
The state of your own hypertext preprocessor
The state of your own hypertext preprocessorThe state of your own hypertext preprocessor
The state of your own hypertext preprocessorAlessandro Nadalin
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency InjectionRifat Nabi
 

Similar to Gérer vos objets (20)

How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et Pimple
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricks
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmers
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
 
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
 
Be pragmatic, be SOLID
Be pragmatic, be SOLIDBe pragmatic, be SOLID
Be pragmatic, be SOLID
 
Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5
 
How kris-writes-symfony-apps-london
How kris-writes-symfony-apps-londonHow kris-writes-symfony-apps-london
How kris-writes-symfony-apps-london
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your Code
 
The state of your own hypertext preprocessor
The state of your own hypertext preprocessorThe state of your own hypertext preprocessor
The state of your own hypertext preprocessor
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
PHP code examples
PHP code examplesPHP code examples
PHP code examples
 

Recently uploaded

Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 

Recently uploaded (20)

Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 

Gérer vos objets

  • 1. thomas@gasc.fr @methylbro Manage your objects Tuesday 9th of September 2014 Montpellier
  • 2. /* ask name */ fwrite(STDOUT, "enter your name :"); $name = trim(fgets(STDIN, 255)); /* ask birthday */ fwrite(STDOUT, "enter your birthday (yyyy/mm/dd) :"); $birthday = new DateTime(trim(fgets(STDIN, 255))); /* calculate the age */ $age = $birthday->diff(new DateTime()); Case study /* display the result */ fwrite( STDOUT, sprintf(PHP_EOL."hello %s you've got %s years old.", $name, $age->y) );
  • 4. Reader + handle + length + read(msg) Writer + handle + write(msg) AgeCalculator + execute() Application Class Diagram
  • 5. // src/Reader.php class Reader { private $handle = STDIN; private $length = 255; private $writer; public function __construct() { $this->writer = new Writer(); } public function read($msg) { $this->writer->write($msg); return trim(fgets($this->handle, $this->length)); } }
  • 6. // src/AgeCalculator.php class AgeCalculator { private $reader, $writer; public function __construct() { $this->reader = new Reader(); $this->writer = new Writer(); } public function execute() { $name = $this->reader->read('your name:'); $birthday = new DateTime( $this->reader->read('your birthday (yyyy/mm/dd):') ); $age = $birthday->diff(new DateTime('now')); $this->writer->write( sprintf("hello %s you've got %s years old.", $name, $age->y) ); } }
  • 7. // age-calculator.php $ageCalculator = new AgeCalculator(); $ageCalculator->execute(); $ php age-calculator.php your name: thomas your birthday (yyyy/mm/dd): 1994/01/01 hello thomas you've got 20 years old. Running the application
  • 8. Dependency Injection Pass a dependency to a dependent object
  • 9. // src/Reader.php class Reader { private $handle, $length, $writer; public function __construct(Writer $writer = null, $handle = STDIN, $length = 255) { $this->handle = $handle; $this->length = $length; $this->writer = $writer; } public function read($msg) { if ($this->writer) { $this->writer->write($msg); } return trim(fgets($this->handle, $this->length)); } }
  • 10. // age-calculator.php $writer = new Writer(); $reader = new Reader($writer); $ageCalculator = new AgeCalculator($writer, $reader); $ageCalculator->execute();
  • 11. // age-calculator.php $writer = new Writer(fopen('result', 'w+')); $reader = new Reader(null, fopen('data', 'r')); $ageCalculator = new AgeCalculator($writer, $reader); $ageCalculator->execute();
  • 12. Using a Dependency Injection Framework
  • 13. // config/cli.php return [ 'Writer' => DIobject()->constructor(), 'Reader' => DIobject() ->constructor(DIlink('Writer')), 'AgeCalculator' => DIobject() ->constructor(DIlink('Writer'), DIlink('Reader')), ];
  • 14. // age-calculator.php require 'vendor/autoload.php'; $builder = new DIContainerBuilder(); $builder->addDefinitions('config/cli.php'); $container = $builder->build(); $ageCalculator = $container->get('AgeCalculator'); $ageCalculator->execute();
  • 15. // config/file.php return [ 'handle.result' => fopen('result', 'w+'), 'handle.data' => fopen('data', 'r'), 'Writer' => DIobject() ->constructor(DIlink('handle.result')), 'Reader' => DIobject() ->constructor(null, DIlink('handle.data')), 'AgeCalculator' => DIobject() ->constructor(DIlink('Writer'), DIlink('Reader')), ];
  • 16. Factory Design Pattern Deals with the problem of creating objects without specifying the exact class of object that will be created
  • 17. << abstract >> Factory ReaderFactory WriterFactory + create() creates creates + write(msg) FileWriter uses Reader Writer << interface >> ReaderInterface + setWriter(writer) + read(msg) FileReader << interface >> WriterInterface extends extends extends extends extends extends
  • 18. // src/FileWriter.php class FileWriter extends Writer { public function __construct($filename, $length = 255) { $this->handle = fopen($filename, 'w+'); $this->length = $length; } } // src/Filereader.php class FileReader extends Reader { public function __construct($filename, $length = 255) { $this->handle = fopen($filename, 'r'); $this->length = $length; } }
  • 19. // src/Factory.php abstract class Factory { private $class; public function __construct($class) { $this->class = new ReflectionClass($class); } protected abstract function configure(ReflectionClass $class); public function create($args = array()) { return $this->class->newInstanceArgs($args); } }
  • 20. // config/file.php return [ 'writer.class' => 'FileWriter', 'reader.class' => 'FileReader', 'data.file' => 'data', 'result.file' => 'result', 'WriterFactory' => DIobject()->constructor(DIlink('writer.class')), 'ReaderFactory' => DIobject() ->constructor( DIlink('reader.class')), DIfactory(function ($container) { return $container ->get('WriterFactory') ->create(array($container->get('result.file'))) ; }), DIfactory(function ($container) { return $container ->get('ReaderFactory') ->create(array($container->get('data.file'))) ; } ), 'AgeCalculator' => DIobject()->constructor(DIlink('writer'), DIlink('reader')), ];
  • 21. Singleton Design Pattern Restricts the instantiation of a class to one object
  • 23. Questions ? Full examples https://github.com/methylbro/human-talk-manage-objects-examples