Web Design
and
Development
By: Shahrzad Peyman
Session 9
December-2017
shahrzad.peymaan@gmail.com
1
2
Today’s Presentation
• self
• Inheritance
• Access Level
• Interface
• Abstract
• Trait
Constructor
3
When creating a new object, you can pass a list
of arguments to the class being called. These are
passed to a special method within the class,
called the constructor, which initializes various
properties.
Static Method
4
You can also define a method as static, which
means that it is called on a class, not on an
object. A static method has no access to any
object properties and is created and accessed as
in below example:
Declaring Constants
5
You can define constants inside classes. The
generally accepted practice is to use uppercase
letters to make them stand out.
Encapsulation
6
In basic terms, it’s the way we define the visibility of
our properties and methods.
• When you’re creating classes, you have to ask
yourself what properties and methods can be
accessed outside of the class.
Access Level
7
Specifier Class SubClass all
Private *
Protected * *
Public * * *
Inheritance
• In object-oriented programming, inheritance
is when a class is based on another class,
using the same implementation or specifying
a new implementation to maintain the same
behavior.
• Such an inherited class is called a subclass
of its parent class or super class.
8
Inheritance
9
Inheritance
10
11
Interface
An interface is an outline of what a particular
object can do. You will often hear that an
interface is a contract. This is true in as much
as an interface defines the public methods that
a class must implement.
Object interfaces allow you to create code
which specifies which methods a class must
implement, without having to define how these
methods are handled.
Interface
12
Abstract
13
An abstract class is a special type of class that can't
be instantiated — in other words, you can't create
objects from it. Instead, you create child classes
from the abstract class, and create objects from
those child classes instead.
An abstract class is designed to be used as a
template for creating classes.
An abstract class will have abstract methods which
are defined by the abstract keyword, these methods
are like the methods defined in the interface classes.
14
Abstract
Interface vs Abstract
Interface Abstract
The Code
• abstract methods
• constants
• abstract methods
• constants
• concrete methods
• concrete variables
Access Modifiers • public
• public
• private
• protected
Number of Parents
Class can impelement
more than one interface
Class can inherit only
from one abstract class
15
Trait
16
One of the problems of PHP as a programming
language is the fact that you can only have
single inheritance. This means a class can only
inherit from one other class.
However, a lot of the time it would be beneficial
to inherit from multiple classes. For example, it
might be desirable to inherit methods from a
couple of different classes in order to prevent
code duplication.
17
Trait
A Trait is simply a group of methods that you
want include within another class.

Web Design & Development - Session 9

  • 1.
    Web Design and Development By: ShahrzadPeyman Session 9 December-2017 shahrzad.peymaan@gmail.com 1
  • 2.
    2 Today’s Presentation • self •Inheritance • Access Level • Interface • Abstract • Trait
  • 3.
    Constructor 3 When creating anew object, you can pass a list of arguments to the class being called. These are passed to a special method within the class, called the constructor, which initializes various properties.
  • 4.
    Static Method 4 You canalso define a method as static, which means that it is called on a class, not on an object. A static method has no access to any object properties and is created and accessed as in below example:
  • 5.
    Declaring Constants 5 You candefine constants inside classes. The generally accepted practice is to use uppercase letters to make them stand out.
  • 6.
    Encapsulation 6 In basic terms,it’s the way we define the visibility of our properties and methods. • When you’re creating classes, you have to ask yourself what properties and methods can be accessed outside of the class.
  • 7.
    Access Level 7 Specifier ClassSubClass all Private * Protected * * Public * * *
  • 8.
    Inheritance • In object-orientedprogramming, inheritance is when a class is based on another class, using the same implementation or specifying a new implementation to maintain the same behavior. • Such an inherited class is called a subclass of its parent class or super class. 8
  • 9.
  • 10.
  • 11.
    11 Interface An interface isan outline of what a particular object can do. You will often hear that an interface is a contract. This is true in as much as an interface defines the public methods that a class must implement. Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
  • 12.
  • 13.
    Abstract 13 An abstract classis a special type of class that can't be instantiated — in other words, you can't create objects from it. Instead, you create child classes from the abstract class, and create objects from those child classes instead. An abstract class is designed to be used as a template for creating classes. An abstract class will have abstract methods which are defined by the abstract keyword, these methods are like the methods defined in the interface classes.
  • 14.
  • 15.
    Interface vs Abstract InterfaceAbstract The Code • abstract methods • constants • abstract methods • constants • concrete methods • concrete variables Access Modifiers • public • public • private • protected Number of Parents Class can impelement more than one interface Class can inherit only from one abstract class 15
  • 16.
    Trait 16 One of theproblems of PHP as a programming language is the fact that you can only have single inheritance. This means a class can only inherit from one other class. However, a lot of the time it would be beneficial to inherit from multiple classes. For example, it might be desirable to inherit methods from a couple of different classes in order to prevent code duplication.
  • 17.
    17 Trait A Trait issimply a group of methods that you want include within another class.