‫‪PHP Egypt‬‬       ‫پي اتش پي مص‬

                   ‫برعاية‬


 ‫لماذا و كيف تكتب إطار عمل‬
‫‪Why & How to make your own framework‬‬


                                 ‫تقديم‬
                            ‫هاني جمال‬
What is a framework           ‫ما هو إطار العمل‬

    A collection of software libraries
     providing a defined application
     programming interface (API).
    (API) is a specified source code to be
     used as a communication interface
     between software components.
    The (API) includes specifications for
     routines, data structures, object classes,
     and variables.
And I Say ...

✔   The Software Framework is
    about finding common
    solutions for difficult
    problems”
‫لماذا؟‬                               Why?

   Security Matters
       It is secured because you made it
   License
       To define your own license
   Practice not to use but to learn :)
       Improves lack-of-time decision making
       Thinking in terms of API improves code quality
   Great Asset
       Reusable things are corporate assets
‫استراحة للمناقشة‬
‫مباديء و معايير عامة‬

   Good code is modular
   Easy to read, to learn and to use
   Hard to misuse
   Can meet requirements
   Easy to extend
‫مباديء و معايير عامة‬

   Focus on gathered requirements
   Extract the true requirements use-cases
   Build general & generic
   Code lives on as examples and unit tests
   Build a Service Provider Interface (SPI)
   Make sure that better solutions exists
   Expect to make mistakes
‫مباديء و معايير عامة‬

   Your API should do one thing and do it well
       Functionality should be easy to explain
       Should be as small as possible but not smaller
       You can always add but you can never remove
       API is a little language, so be consistent, regular
        and strive for symmetry
        if (car.speed() > SPEED_LIMIT)
          createAlert(”Watch out for cops!”);
   ”You'll either step forward in growth or will
    step back into safety” --Abraham Maslow
‫مباديء و معايير عامة‬

   Implementation should not impact API
       Ex. don't specify hash functions
   Minimize accessibility of everything
       Make classes and members as private as
        possible
       Public classes should have no public fields
        except the constants
       This maximizes information hiding and allows
        modules to be used, understood, built, tested and
        debugged independently
‫مباديء و معايير عامة‬

   Reuse is easier to say then to do
       Reuse = Good Design + Very Good Documentation
       Document every class, interface, method,
        constructor, parameter and exception
       Obey standard naming conventions
       Return types and avoid obsolete parameters
       Simulate patterns of the language
Class Design

 Simple
 Thread-safe


 Reusable
Class Design

   Subclass only when it makes sense
    otherwise use composition
   Public classes should not subclass other
    public classes for ease of implementation


     ”if you allow subclassing, how do methods
                  use one another?”
Method Design

   Don't make the client do anything the
    module could do
   Reduce the need for copy-paste
   Strong typing is the best
   Provide programmatic access to all data
    available in string form
       Clients should never parse strings
   Overload carefully
Method design

   Use appropriate parameter and return types
   Don't use string if a better type exists
   Don't use floating point for monetary values
       Binary floating causes inexact results!
   Use double(64 bits) instead of float(32 bits)
   Use consistent parameter ordering across methods
   Avoid long parameter list, three or less :)
   Avoid return values that demand exceptional
    processing
Exception Design

   Throw exceptions to indicate exceptional
    conditions only!
   Don't force client to use exceptions for
    control flow!
   Do silent exceptions so client won't be
    forced to take a recovery action
   Include failure-capture information in
    exceptions to allow providing repair or
    recovery
‫شكرًا‬

Php Egypt Jan14

  • 1.
    ‫‪PHP Egypt‬‬ ‫پي اتش پي مص‬ ‫برعاية‬ ‫لماذا و كيف تكتب إطار عمل‬ ‫‪Why & How to make your own framework‬‬ ‫تقديم‬ ‫هاني جمال‬
  • 2.
    What is aframework ‫ما هو إطار العمل‬  A collection of software libraries providing a defined application programming interface (API).  (API) is a specified source code to be used as a communication interface between software components.  The (API) includes specifications for routines, data structures, object classes, and variables.
  • 3.
    And I Say... ✔ The Software Framework is about finding common solutions for difficult problems”
  • 4.
    ‫لماذا؟‬ Why?  Security Matters  It is secured because you made it  License  To define your own license  Practice not to use but to learn :)  Improves lack-of-time decision making  Thinking in terms of API improves code quality  Great Asset  Reusable things are corporate assets
  • 5.
  • 6.
    ‫مباديء و معاييرعامة‬  Good code is modular  Easy to read, to learn and to use  Hard to misuse  Can meet requirements  Easy to extend
  • 7.
    ‫مباديء و معاييرعامة‬  Focus on gathered requirements  Extract the true requirements use-cases  Build general & generic  Code lives on as examples and unit tests  Build a Service Provider Interface (SPI)  Make sure that better solutions exists  Expect to make mistakes
  • 8.
    ‫مباديء و معاييرعامة‬  Your API should do one thing and do it well  Functionality should be easy to explain  Should be as small as possible but not smaller  You can always add but you can never remove  API is a little language, so be consistent, regular and strive for symmetry if (car.speed() > SPEED_LIMIT) createAlert(”Watch out for cops!”);  ”You'll either step forward in growth or will step back into safety” --Abraham Maslow
  • 9.
    ‫مباديء و معاييرعامة‬  Implementation should not impact API  Ex. don't specify hash functions  Minimize accessibility of everything  Make classes and members as private as possible  Public classes should have no public fields except the constants  This maximizes information hiding and allows modules to be used, understood, built, tested and debugged independently
  • 10.
    ‫مباديء و معاييرعامة‬  Reuse is easier to say then to do  Reuse = Good Design + Very Good Documentation  Document every class, interface, method, constructor, parameter and exception  Obey standard naming conventions  Return types and avoid obsolete parameters  Simulate patterns of the language
  • 11.
    Class Design  Simple Thread-safe  Reusable
  • 12.
    Class Design  Subclass only when it makes sense otherwise use composition  Public classes should not subclass other public classes for ease of implementation ”if you allow subclassing, how do methods use one another?”
  • 13.
    Method Design  Don't make the client do anything the module could do  Reduce the need for copy-paste  Strong typing is the best  Provide programmatic access to all data available in string form  Clients should never parse strings  Overload carefully
  • 14.
    Method design  Use appropriate parameter and return types  Don't use string if a better type exists  Don't use floating point for monetary values  Binary floating causes inexact results!  Use double(64 bits) instead of float(32 bits)  Use consistent parameter ordering across methods  Avoid long parameter list, three or less :)  Avoid return values that demand exceptional processing
  • 15.
    Exception Design  Throw exceptions to indicate exceptional conditions only!  Don't force client to use exceptions for control flow!  Do silent exceptions so client won't be forced to take a recovery action  Include failure-capture information in exceptions to allow providing repair or recovery
  • 16.