SlideShare a Scribd company logo
1 of 10
Ending Rails Envy in PHP5
                          __call for fun and profit!




   Slides with notes at http://www.siverstripe.com/rails-envy/




                                Who am I? Oooh, a white slide!




    Sam Minnée
    CTO
    SilverStripe Limited

                                                             http://www.silverstripe.com




  The concepts discussed today have been used extensively
  in our BSD-licensed CMS, in production use on around 100
  sites.
SilverStripe is a BSD-licensed CMS focused on providing a productive development framework on top
of a CMS that it aimed squarely at content authors, not developers.

http://www.silverstripe.com

It’s relatively new (first open-sourced November 2006) but our developer community is growing well
and we’re always looking for new developers.
Why the Envy?
                    PHP                                              Rails

                                                       Productive development
      Gets the job done, mostly
                                                    platform with lots of cool stuff

 The language people love to hate                                 Very trendy


     A variety of hosting options                              Harder to Host

       Good NZ government
                                                        Too new for government
     acceptance (They like OSS!)
Rails was one of the main options I considered when developing SilverStripe. However, there were
other advantages to PHP that trumped it - in short, Rails would cut down our user-base too much.




                             What to do?


            •   Use PHP5
            •   Build frameworks to make it productive, and
                maybe even trendy.




We decided to use PHP, but focus on building a productive development framework, a similar goal to
the Rails team.
DISCLAIMER
               THIS IS NOT A LANGUAGE WAR


              I know very little about the details of Rails vs.
              what is presented today, in fact, I’m pretty sure
              that my understanding of the benefits of using
                           Rails is pretty naïve.
                     I just thought the title would be edgy.
                                          Forgive me.


                                                   (besides, I still envy Rails. Just a little.)




                                      __call()
       The possiblities are endless with this handy little method...

        It lets us create “magic methods” are are implicitly defined.




The __call() method of a class will handle any unrecognised method call. Similar methods for
properties, __get() and __set(), can be used to create “magic properties” too.
Object-Relational Models
                                      The first use-case

                             The way that I want it to be:




An ORM is a great way of wrapping access to a database into a nice package. We wanted to add
relationship information to our data classes, and have these accessible by set classes that implement
Iterator, but need more

The code shown in the presentation is criminally oversimplified. In particular, in a real implementation
we would want to use For a more complete implementation, the SilverStripe codebase is a good place
to look.




                           First Approach




Here we have defined a __call() method on the DataObject that
Problems

           •   __call contains a lot of program-specific
               information.
           •   The if / elseif construct lower performance as
               it grows
           •   Won’t scale well to more kinds of magic
               functions.




                    Second Approach
                              Map of method handlers




Define methods makes a number of calls to $this->addWrapperMethod(). This is how you define your
magic methods.
Second Approach
                            __call() implemented in Object




The addWrapperMethod() method, and the __call() handler, are defined in a generic Object class.

* addWrapperMethod() adds to Object::$extraMethods
* __call() interrogates Object::$extraMethods to know what to do.




              Calling defineMethods
      Once per class, not once per object, for performance.




Calling defineMethods() for every object can get really slow. You typically have to instanciate a large
number of DataObjects to generate a single page.
Aggregation
                        Adding Versioning to DataObject




                                                                    Note: Criminally Oversimplified


This is a very simple illustration of how aggregation might be used to help implement versioning &
staging. In this case, we’ve just used it to add publish() and rollback().

In a real implementation, you need a way of modifying the code for reading/writing DataObjects to
actually change the data model. But that is beyond the scope of this talk.




  What can defineMethods do?

     •    addWrapperMethod(‘Products’, ‘getComponents’);
     •    addMethodsFrom(‘extensions’, ‘Versioned’);
     •    Other possible
     •    BUT: the more we have, the slower things get... So be
          careful!




In addition to addWrapperMethod(), it’s worth adding addMethodsFrom()

Performance in the __call() method is crucial because it will be used so often by your application.
Adding addMethodsFrom




Step 1: Implement addMethodsFrom() that will be called by defineMethod(). It should add more stu! to
Object::$extraMethods




         Adding addMethodsFrom




Step 2: Update __call() to handle the new data that we’ve put into Object::$extraMethods

Note: this isn’t a particularly well optimised sample. Improving on it is left as an exercise for the
reader ;-)
Implementing Aggregation




We can now use the addMethodsFrom() methods to implement aggregation, by updating
defineMethods() in the Object class.




                           Why Bother?


           •   I’m Lazy
           •   I’m Picky
           •   (I like over-engineering?)
More Information
http://www.silverstripe.com/rails-envy/


•   Copy of the slides
•   Sample code
•   More notes
•   Or, email me on sam@silverstripe.com


             Thank you for listening!

More Related Content

Similar to Os Minnee

Rails Vs CakePHP
Rails Vs CakePHPRails Vs CakePHP
Rails Vs CakePHPGautam Rege
 
Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The EnterpriseMatt Aimonetti
 
Viridians on Rails
Viridians on RailsViridians on Rails
Viridians on RailsViridians
 
Ruby On Rails Presentation
Ruby On Rails PresentationRuby On Rails Presentation
Ruby On Rails PresentationPaul Pajo
 
Ruby on Rails - An overview
Ruby on Rails -  An overviewRuby on Rails -  An overview
Ruby on Rails - An overviewThomas Asikis
 
Top 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and AnswersTop 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and AnswersVineet Kumar Saini
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteDr Nic Williams
 
Building Drupal 8 Sites
Building Drupal 8 SitesBuilding Drupal 8 Sites
Building Drupal 8 SitesExove
 
Deep into Drupal Theming Layer
Deep into Drupal Theming LayerDeep into Drupal Theming Layer
Deep into Drupal Theming Layerinsready
 
30 Skills to Master to Become a Senior Software Engineer
30 Skills to Master to Become a Senior Software Engineer30 Skills to Master to Become a Senior Software Engineer
30 Skills to Master to Become a Senior Software EngineerSean Coates
 
Enterprise Architectures with Ruby (and Rails)
Enterprise Architectures with Ruby (and Rails)Enterprise Architectures with Ruby (and Rails)
Enterprise Architectures with Ruby (and Rails)Konstantin Gredeskoul
 
Creating Semantic Mashups Bridging Web 2 0 And The Semantic Web Presentation 1
Creating Semantic Mashups  Bridging Web 2 0 And The Semantic Web Presentation 1Creating Semantic Mashups  Bridging Web 2 0 And The Semantic Web Presentation 1
Creating Semantic Mashups Bridging Web 2 0 And The Semantic Web Presentation 1jward5519
 
Creating Semantic Mashups Bridging Web 2 0 And The Semantic Web Presentation 1
Creating Semantic Mashups  Bridging Web 2 0 And The Semantic Web Presentation 1Creating Semantic Mashups  Bridging Web 2 0 And The Semantic Web Presentation 1
Creating Semantic Mashups Bridging Web 2 0 And The Semantic Web Presentation 1jward5519
 
DiUS Computing Lca Rails Final
DiUS  Computing Lca Rails FinalDiUS  Computing Lca Rails Final
DiUS Computing Lca Rails FinalRobert Postill
 
What Web Framework To Use?
What Web Framework To Use?What Web Framework To Use?
What Web Framework To Use?Kasra Khosravi
 
Workshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfWorkshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfTobiasGoeschel
 
Chapter15-Presentation.pptx
Chapter15-Presentation.pptxChapter15-Presentation.pptx
Chapter15-Presentation.pptxGFRomano
 
Core Data in RubyMotion #inspect
Core Data in RubyMotion #inspectCore Data in RubyMotion #inspect
Core Data in RubyMotion #inspectLori Olson
 

Similar to Os Minnee (20)

Rails Vs CakePHP
Rails Vs CakePHPRails Vs CakePHP
Rails Vs CakePHP
 
Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The Enterprise
 
Viridians on Rails
Viridians on RailsViridians on Rails
Viridians on Rails
 
Ruby On Rails Presentation
Ruby On Rails PresentationRuby On Rails Presentation
Ruby On Rails Presentation
 
Ruby on Rails - An overview
Ruby on Rails -  An overviewRuby on Rails -  An overview
Ruby on Rails - An overview
 
Ruby Metaprogramming 08
Ruby Metaprogramming 08Ruby Metaprogramming 08
Ruby Metaprogramming 08
 
Rails Concept
Rails ConceptRails Concept
Rails Concept
 
Top 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and AnswersTop 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and Answers
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
 
Building Drupal 8 Sites
Building Drupal 8 SitesBuilding Drupal 8 Sites
Building Drupal 8 Sites
 
Deep into Drupal Theming Layer
Deep into Drupal Theming LayerDeep into Drupal Theming Layer
Deep into Drupal Theming Layer
 
30 Skills to Master to Become a Senior Software Engineer
30 Skills to Master to Become a Senior Software Engineer30 Skills to Master to Become a Senior Software Engineer
30 Skills to Master to Become a Senior Software Engineer
 
Enterprise Architectures with Ruby (and Rails)
Enterprise Architectures with Ruby (and Rails)Enterprise Architectures with Ruby (and Rails)
Enterprise Architectures with Ruby (and Rails)
 
Creating Semantic Mashups Bridging Web 2 0 And The Semantic Web Presentation 1
Creating Semantic Mashups  Bridging Web 2 0 And The Semantic Web Presentation 1Creating Semantic Mashups  Bridging Web 2 0 And The Semantic Web Presentation 1
Creating Semantic Mashups Bridging Web 2 0 And The Semantic Web Presentation 1
 
Creating Semantic Mashups Bridging Web 2 0 And The Semantic Web Presentation 1
Creating Semantic Mashups  Bridging Web 2 0 And The Semantic Web Presentation 1Creating Semantic Mashups  Bridging Web 2 0 And The Semantic Web Presentation 1
Creating Semantic Mashups Bridging Web 2 0 And The Semantic Web Presentation 1
 
DiUS Computing Lca Rails Final
DiUS  Computing Lca Rails FinalDiUS  Computing Lca Rails Final
DiUS Computing Lca Rails Final
 
What Web Framework To Use?
What Web Framework To Use?What Web Framework To Use?
What Web Framework To Use?
 
Workshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfWorkshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdf
 
Chapter15-Presentation.pptx
Chapter15-Presentation.pptxChapter15-Presentation.pptx
Chapter15-Presentation.pptx
 
Core Data in RubyMotion #inspect
Core Data in RubyMotion #inspectCore Data in RubyMotion #inspect
Core Data in RubyMotion #inspect
 

More from oscon2007

J Ruby Whirlwind Tour
J Ruby Whirlwind TourJ Ruby Whirlwind Tour
J Ruby Whirlwind Touroscon2007
 
Solr Presentation5
Solr Presentation5Solr Presentation5
Solr Presentation5oscon2007
 
Os Fitzpatrick Sussman Wiifm
Os Fitzpatrick Sussman WiifmOs Fitzpatrick Sussman Wiifm
Os Fitzpatrick Sussman Wiifmoscon2007
 
Performance Whack A Mole
Performance Whack A MolePerformance Whack A Mole
Performance Whack A Moleoscon2007
 
Os Lanphier Brashears
Os Lanphier BrashearsOs Lanphier Brashears
Os Lanphier Brashearsoscon2007
 
Os Fitzpatrick Sussman Swp
Os Fitzpatrick Sussman SwpOs Fitzpatrick Sussman Swp
Os Fitzpatrick Sussman Swposcon2007
 
Os Berlin Dispelling Myths
Os Berlin Dispelling MythsOs Berlin Dispelling Myths
Os Berlin Dispelling Mythsoscon2007
 
Os Keysholistic
Os KeysholisticOs Keysholistic
Os Keysholisticoscon2007
 
Os Jonphillips
Os JonphillipsOs Jonphillips
Os Jonphillipsoscon2007
 
Os Urnerupdated
Os UrnerupdatedOs Urnerupdated
Os Urnerupdatedoscon2007
 

More from oscon2007 (20)

J Ruby Whirlwind Tour
J Ruby Whirlwind TourJ Ruby Whirlwind Tour
J Ruby Whirlwind Tour
 
Solr Presentation5
Solr Presentation5Solr Presentation5
Solr Presentation5
 
Os Borger
Os BorgerOs Borger
Os Borger
 
Os Harkins
Os HarkinsOs Harkins
Os Harkins
 
Os Fitzpatrick Sussman Wiifm
Os Fitzpatrick Sussman WiifmOs Fitzpatrick Sussman Wiifm
Os Fitzpatrick Sussman Wiifm
 
Os Bunce
Os BunceOs Bunce
Os Bunce
 
Yuicss R7
Yuicss R7Yuicss R7
Yuicss R7
 
Performance Whack A Mole
Performance Whack A MolePerformance Whack A Mole
Performance Whack A Mole
 
Os Fogel
Os FogelOs Fogel
Os Fogel
 
Os Lanphier Brashears
Os Lanphier BrashearsOs Lanphier Brashears
Os Lanphier Brashears
 
Os Tucker
Os TuckerOs Tucker
Os Tucker
 
Os Fitzpatrick Sussman Swp
Os Fitzpatrick Sussman SwpOs Fitzpatrick Sussman Swp
Os Fitzpatrick Sussman Swp
 
Os Furlong
Os FurlongOs Furlong
Os Furlong
 
Os Berlin Dispelling Myths
Os Berlin Dispelling MythsOs Berlin Dispelling Myths
Os Berlin Dispelling Myths
 
Os Kimsal
Os KimsalOs Kimsal
Os Kimsal
 
Os Pruett
Os PruettOs Pruett
Os Pruett
 
Os Alrubaie
Os AlrubaieOs Alrubaie
Os Alrubaie
 
Os Keysholistic
Os KeysholisticOs Keysholistic
Os Keysholistic
 
Os Jonphillips
Os JonphillipsOs Jonphillips
Os Jonphillips
 
Os Urnerupdated
Os UrnerupdatedOs Urnerupdated
Os Urnerupdated
 

Recently uploaded

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 

Recently uploaded (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 

Os Minnee

  • 1. Ending Rails Envy in PHP5 __call for fun and profit! Slides with notes at http://www.siverstripe.com/rails-envy/ Who am I? Oooh, a white slide! Sam Minnée CTO SilverStripe Limited http://www.silverstripe.com The concepts discussed today have been used extensively in our BSD-licensed CMS, in production use on around 100 sites. SilverStripe is a BSD-licensed CMS focused on providing a productive development framework on top of a CMS that it aimed squarely at content authors, not developers. http://www.silverstripe.com It’s relatively new (first open-sourced November 2006) but our developer community is growing well and we’re always looking for new developers.
  • 2. Why the Envy? PHP Rails Productive development Gets the job done, mostly platform with lots of cool stuff The language people love to hate Very trendy A variety of hosting options Harder to Host Good NZ government Too new for government acceptance (They like OSS!) Rails was one of the main options I considered when developing SilverStripe. However, there were other advantages to PHP that trumped it - in short, Rails would cut down our user-base too much. What to do? • Use PHP5 • Build frameworks to make it productive, and maybe even trendy. We decided to use PHP, but focus on building a productive development framework, a similar goal to the Rails team.
  • 3. DISCLAIMER THIS IS NOT A LANGUAGE WAR I know very little about the details of Rails vs. what is presented today, in fact, I’m pretty sure that my understanding of the benefits of using Rails is pretty naïve. I just thought the title would be edgy. Forgive me. (besides, I still envy Rails. Just a little.) __call() The possiblities are endless with this handy little method... It lets us create “magic methods” are are implicitly defined. The __call() method of a class will handle any unrecognised method call. Similar methods for properties, __get() and __set(), can be used to create “magic properties” too.
  • 4. Object-Relational Models The first use-case The way that I want it to be: An ORM is a great way of wrapping access to a database into a nice package. We wanted to add relationship information to our data classes, and have these accessible by set classes that implement Iterator, but need more The code shown in the presentation is criminally oversimplified. In particular, in a real implementation we would want to use For a more complete implementation, the SilverStripe codebase is a good place to look. First Approach Here we have defined a __call() method on the DataObject that
  • 5. Problems • __call contains a lot of program-specific information. • The if / elseif construct lower performance as it grows • Won’t scale well to more kinds of magic functions. Second Approach Map of method handlers Define methods makes a number of calls to $this->addWrapperMethod(). This is how you define your magic methods.
  • 6. Second Approach __call() implemented in Object The addWrapperMethod() method, and the __call() handler, are defined in a generic Object class. * addWrapperMethod() adds to Object::$extraMethods * __call() interrogates Object::$extraMethods to know what to do. Calling defineMethods Once per class, not once per object, for performance. Calling defineMethods() for every object can get really slow. You typically have to instanciate a large number of DataObjects to generate a single page.
  • 7. Aggregation Adding Versioning to DataObject Note: Criminally Oversimplified This is a very simple illustration of how aggregation might be used to help implement versioning & staging. In this case, we’ve just used it to add publish() and rollback(). In a real implementation, you need a way of modifying the code for reading/writing DataObjects to actually change the data model. But that is beyond the scope of this talk. What can defineMethods do? • addWrapperMethod(‘Products’, ‘getComponents’); • addMethodsFrom(‘extensions’, ‘Versioned’); • Other possible • BUT: the more we have, the slower things get... So be careful! In addition to addWrapperMethod(), it’s worth adding addMethodsFrom() Performance in the __call() method is crucial because it will be used so often by your application.
  • 8. Adding addMethodsFrom Step 1: Implement addMethodsFrom() that will be called by defineMethod(). It should add more stu! to Object::$extraMethods Adding addMethodsFrom Step 2: Update __call() to handle the new data that we’ve put into Object::$extraMethods Note: this isn’t a particularly well optimised sample. Improving on it is left as an exercise for the reader ;-)
  • 9. Implementing Aggregation We can now use the addMethodsFrom() methods to implement aggregation, by updating defineMethods() in the Object class. Why Bother? • I’m Lazy • I’m Picky • (I like over-engineering?)
  • 10. More Information http://www.silverstripe.com/rails-envy/ • Copy of the slides • Sample code • More notes • Or, email me on sam@silverstripe.com Thank you for listening!