Top Banner
Advanced PHP Concepts Tutorial One
13

Advanced PHP Concepts - Tutorial 1 of 3

Apr 12, 2017

Download

Technology

James Whitehead
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advanced PHP Concepts - Tutorial 1 of 3

Advanced PHP ConceptsTutorial One

Page 2: Advanced PHP Concepts - Tutorial 1 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 1 of 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.

Page 4: Advanced PHP Concepts - Tutorial 1 of 3

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.

Page 5: Advanced PHP Concepts - Tutorial 1 of 3

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.

Page 6: Advanced PHP Concepts - Tutorial 1 of 3

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?

Page 7: Advanced PHP Concepts - Tutorial 1 of 3
Page 8: Advanced PHP Concepts - Tutorial 1 of 3

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…

Page 9: Advanced PHP Concepts - Tutorial 1 of 3
Page 10: Advanced PHP Concepts - Tutorial 1 of 3

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.

Page 11: Advanced PHP Concepts - Tutorial 1 of 3

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

Page 12: Advanced PHP Concepts - Tutorial 1 of 3

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