Advanced PHP Concepts - Tutorial 2 of 3

13
Advanced PHP Concepts Tutorial Two

Transcript of Advanced PHP Concepts - Tutorial 2 of 3

Page 1: Advanced PHP Concepts - Tutorial 2 of 3

Advanced PHP ConceptsTutorial Two

Page 2: Advanced PHP Concepts - Tutorial 2 of 3

Series Overview• Object-Oriented Programming• Introduction to Objects• Basic OOP Classes

• OOP Principles• MVC• SOLID• Namespaces

• Frameworks• Symfony 3

Page 3: Advanced PHP Concepts - Tutorial 2 of 3

MVC• Model-View-Controller• This architecture has

become extremely popular for designing web applications.

Page 4: Advanced PHP Concepts - Tutorial 2 of 3

MVC (cont.)• A controller can send commands to the model to

update the model's state (e.g., editing a document). It can also send commands to its associated view to change the view's presentation of the model (e.g., by scrolling through a document).• A model stores data that is retrieved according to

commands from the controller and displayed in the view.• A view generates an output presentation to the user

based on changes in the model.• A view controller generates an output view and an

embedded controller.

Page 5: Advanced PHP Concepts - Tutorial 2 of 3

SOLID• Single responsibility principle

• A class should have only a single responsibility• Open/closed principle

• Software entities should be open for extension, but closed for modification• Liskov substitution principle

• Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program

• Interface segregation principle• Many client-specific interfaces are better than one general-purpose interface

• Dependency inversion principle• One should “Depend upon Abstractions. Do not depend upon concretions.”

Page 6: Advanced PHP Concepts - Tutorial 2 of 3

Other OOP Principles• DRY (Don’t Repeat Yourself)• KISS (Keep It Simple, Stupid)

Page 7: Advanced PHP Concepts - Tutorial 2 of 3

Namespaces• Namespaces are commonly structured as hierarchies

to allow reuse of names in different contexts.• In computer programming, namespaces are typically

employed for the purpose of grouping symbols and identifiers around a particular functionality and to avoid name collisions between multiple identifiers that share the same name.• A namespace is defined by the namespace keyword.

Page 8: Advanced PHP Concepts - Tutorial 2 of 3

Why use namespaces?• Often when you are working on a very large project you will

encounter naming collisions between Classes.• Using namespaces solves this problem, as well as giving

your code better structure by grouping all Classes of the same module together in one namespace, organised by their purpose.• Examples:• AppBundle\Controller• AppBundle\Repository

• Let’s revisit our Examples project from the previous tutorial…

Page 9: Advanced PHP Concepts - Tutorial 2 of 3

ExamplesBefore

After Refactoring

Page 10: Advanced PHP Concepts - Tutorial 2 of 3

Summary• MVC – Model-View-Controller

• Model updates View User sees View User uses Controller Controller manipulates Model Model updates View

• SOLID Principles• Single responsibility principle• Open/closed principle• Liskov substitution principle• Interface segregation principle• Dependency inversion principle

• Namespaces• Using namespaces gives your code better structure by grouping all

Classes of the same module together in one namespace, organised by their purpose.

Page 11: Advanced PHP Concepts - Tutorial 2 of 3

Examples available from:https://github.com/WhiteheadJ/Examples