PHP: OOP

Introduction

PHP: OOP

What is OOP?

  • Object-Oriented Programming, a way to organise your code
     
  • Allows you to group similar tasks into containers which are defined using classes, classes are build from objects which contain variables (properties) and functions (methods)
     
  • DRY & KISS

PHP: OOP > Introduction

Why use it?

  • Code becomes:
    • Scalable
    • Modulair
    • Readable
    • Cleaner
       
  • You become a better developer because you:
    • Learn how to organise your code
    • Think in solutions
    • Find it easier to learn other languages

PHP: OOP > Introduction

What is a paradigm?

  • Also called a "pattern" or "model"
     
  • A style or way of programming
     
  • Two main paradigms:
    • procedural
    • object-oriented​

PHP: OOP > Introduction

Classes & objects

PHP: OOP

What is a class?

  • A blueprint for an object
     
  • A group of the code that handles a certain topic into one place
     
  • You use a class to define the properties, methods and behaviour of objects
     
  • In order to interact with a class, we need to create an object from that class
     
  • Declare it with the class keyword

PHP: OOP > Classes & objects

What is an object?

  • The things you create out of a class
     
  • It contains variables and functions

PHP: OOP > Classes & objects

Standards

  • Typically one class per file
     
  • Filename is equal to the classname
     
  • All class files are stored in a class folder
     
  • Start the curly braces on the next line when declaring a class
     
  • Naming convention: StudlyCaps

PHP: OOP > Classes & objects

How to declare a class

PHP: OOP > Classes & objects

<?php

    class Car 
    {
        
    }

How to declare an object

PHP: OOP > Classes & objects

<?php

    // Store the object in a variable and refer to the class
    $car1 = new Car();

    // To view the content of the object
    var_dump($car1);

Result: class and object

PHP: OOP > Classes & objects

<?php

    class Car 
    {
        
    }

    $car1 = new Car();

Properties & methods

PHP: OOP

What is a property?

  • Variables inside a class
     
  • Can only be accessed by using the object
     
  • Properties can accept values like:
    • strings
    • integers
    • booleans
       
  • Must be defined by using access modifiers
     
  • Naming convention: camelCase

PHP: OOP > Properties & methods

What are access modifiers?

  • Allow you to control access or visibility of your properties
     
  • Public:
    • Accessible from anywhere, even from outside the scope of the class
  • Private:
    • Accessed within the class itself, it protects properties and methods from being accessed from outside the class
  • Protected:
    • Works same as private except by allowing child (sub) classes to access protected parent (super) properties and methods

PHP: OOP > Properties & methods

Declaring properties

PHP: OOP > Properties & methods

<?php

    // Public access modifier
    public $foo;

    // Private access modifier
    private $foo;

    // Protected access modifier
    protected $foo;

Access properties

PHP: OOP > Properties & methods

  • Reference the object name, followed by a single arrow and the property name
     
  • Only the object name starts with a $ because it's all seen as one variable
     
  • More than 1 object can be build from the same class at the same time

Result: class, object, property

PHP: OOP > Properties & methods

<?php

    // Class declared with public access modifiers
    class Car 
    {
        public $brand;

        // Parts will be stored in an array
        public $parts = array();

        public $continent = "Europe";
    }

    // Create new object
    $car1 = new Car();
    
    // Print on screen
    echo $car1->continent;
    
    // Visible in console
    $car1->continent;

What is a method?

  • Methods tell an object how to perform certain actions
     
  • A function inside a class is called a method
     
  • Can use $this keyword which indicates we want to use the objects own properties or methods. It allows you to have access to them within the class scope
     
  • Naming convention: camelCase, curly braces on the next line

PHP: OOP > Properties & methods

Declaring a method

PHP: OOP > Properties & methods

<?php

    public function foobar() 
    {
    
        return $this->foo . " is " . $this->bar;
    }

Access methods

PHP: OOP > Properties & methods

  • Works similar as accessing functions and object properties, only difference is that you make a reference to the object it belongs too first

PHP: OOP (Part 1)

By Kim Massaro

PHP: OOP (Part 1)

Basics of Object-Oriented Programming

  • 748