OOP & PHP

What is OOP?

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"). In object-oriented programming, computer programs are designed by making them out of objects that interact with one another.[1][2] 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.

Classes vs Objects

Classes vs Objects

  • class == template
  • objects == instantiated classes

Visibility

  • public
  • private
  • protected

Constructor and Destructor

Copying vs cloning objects

Magic methods

  • __construct()
  •  __destruct()
  •  __call()
  •  __callStatic()
  •  __get()
  •  __set()
  •  __isset()
  •  __unset()
  •  __sleep()
  •  __wakeup()
  •  __toString()
  •  __invoke()
  •  __set_state()
  •  __clone()
  •  __debugInfo()

Magic methods

  • __construct()
  •  __destruct()
  •  __call()
  •  __callStatic()
  •  __get()
  •  __set()
  •  __isset()
  •  __unset()
  •  __sleep()
  •  __wakeup()
  •  __toString()
  •  __invoke()
  •  __set_state()
  •  __clone()
  •  __debugInfo()

Namespaces

  • Before had to watch how you name your class
  • /vagrant/web/app.php
  • namespace keyword

Abstract classes

  • abstract classes cannot be instantiated

Interfaces

  • interface keyword
  • implements keyword

Static Properties

  • scope resolution operator ::
  • accessible without instantiation

Static Methods

  • scope resolution operator ::
  • accessible without instantiation

Static vs Constant

  • const keyword
  • accessible without instantiation
  • static keyword
  • accessible without instantiation

What's the difference?

Late Static Binding

Traits

  • since 5.4.0
  • intended to reuse limitations of single inheritance

Final

  • keyword final
  • prevents further modifications

Type Hinting

Object serialization

$string = serialize($object);

$object = unserialize($string);

  • save to database
  • store in session
  • etc.

PRACTICE

OOP & PHP

By Kruno Knego

OOP & PHP

In house education at Locastic.

  • 660
Loading comments...

More from Kruno Knego