Home / Programming /

PHP OOP

Notes taken at a conference - incomplete

Inheritance

class Person {
    public $first;
    public $last;
}

class Employee extends Person {
    public $title;
}

Abstract classes

abstract class DataStore {
    //These methods must be defined in the child class
    abstract public function save();
    abstract public function load($id);

}
interface Rateable {
    public function rate( int $stars, $user);
    public function getRating();
}
class StatusUpdate implements Rateable, Searchable {
    protected $ratings[];

    public function rate(int $stars, $user) {
        $this->ratings[$user] = $stars;
    }

    public function search($query) {
        // search stuff
    }
}

Traits

trait Counter {
    protected $counter;

    public function increment() {
        $counter ++;
    }

    public funciton getCounter() {
        return $counter;
    }
}

class MyCounter {
    use Counter;
}

$counter = new MyCounter();
$counter->increment();
echo $counter->getCount(); //1

Late Static Binding

Namespaces

<?php
namespace WorkLibrary;

class Database {
    //stuff
}

.....

<?php
namespace MyLibrary;

class Database {
    // stuff
}

Magic Methods


this document last modified: May 12 2018 17:45

Home / Programming /