13
Advanced PHP Concepts Tutorial One

Advanced PHP Concepts - Tutorial 1 of 3

Embed Size (px)

Citation preview

Advanced PHP ConceptsTutorial One

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

• OOP Principles• MVC• SOLID• Namespaces

• Frameworks• Symfony 3

Object-Oriented Programming

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A distinguishing feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self"). In OO programming, computer programs are designed by making them out of objects that interact with one another. There is significant diversity in object-oriented programming, but most popular languages are class-based, meaning that objects are instances of classes, which typically also determines their type.

Objects: The Basics• Model real world “things”.• Holds structured data and can perform actions.• Can “extend” other Objects.• E.g. An Eagle is a Bird. ( class Eagle extends Bird {} )

• Are defined by a Class.• An Object is an instance of that Class.

What is a Class?• Defined by attributes (Class Variables) and behaviours

(Class Methods).• Class attributes and methods must be declared as

either;• Private (or class-private) restricts the access to the class

itself. Only methods that are part of the same class can access private members.• Protected (or class-protected) allows the class itself and all

its subclasses to access the member.• Public means that any code can access the member by its

name.

Example Classes• Let’s think about a Garden Bed as an example of a real life

Object.• What can we typically do with a Garden Bed ?• Plant vegetables in it…• Water it...• Harvest the vegetables...

• We also need a Class to represent our Vegetables.• What information do we need to capture about Vegetables?• Name...• Colour...• Is it Edible?

Putting these Classes to use…• Now that we have written our Classes we can use

them to achieve something.• Let’s model a Person that is attending to their home

garden bed…

Summary• Objects are used to model “things”.• Objects are defined by Classes.• Classes define attributes and behaviours.• Classes can extend and/or use other Classes.• Thinking forward: Objects can perform advanced

behaviours such as providing Services to other parts of your code, or acting as a Repository for data.

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

Further Reading• http://www.phptherightway.com/