PHP OOP
How to define a class:
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function setName($name) {
$this->name = $name;
}
function getName() {
return $this->name;
}
}
The class has properties and methods.
How to define objects:
$apple = new Fruit();
$apple->setName('Apple');
echo $apple->getName();
What is constructor?
You can init default properties to the object when it's created.
<?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function getName() {
return $this->name;
}
}
$apple = new Fruit("Apple");
echo $apple->getName();
Access Modifiers
The methods and properties van have access modifiers. There are 3 type of access modifiers:
public
- the property or method can be accessed from everywhere
protected
- the property or method can be accessed inside the class and it can be inherited
private
- the property or method can be accessed only inside the class
Inheritance
Inherited class come from other class.
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
// Strawberry is inherited from Fruit
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
The final Keyword
The final keyword can be used to prevent class inheritance or to prevent method overriding.
<?php
final class Fruit {
// some code
}
// will result in error
class Strawberry extends Fruit {
// some code
}
?>
<?php
class Fruit {
final public function intro() {
// some code
}
}
class Strawberry extends Fruit {
// will result in error
public function intro() {
// some code
}
}
?>
Class Constants
It's useful if you need to define some constant data within a class.
<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting us";
}
echo Goodbye::LEAVING_MESSAGE;
<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting us";
public function byebye() {
echo self::LEAVING_MESSAGE;
}
}
$goodbye = new Goodbye();
$goodbye->byebye();
Abstract Classes
Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks. An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.
<?php
// Parent class
abstract class Car {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function intro() : string;
}
// Child classes
class Audi extends Car {
public function intro() : string {
return "Choose German quality! I'm an $this->name!";
}
}
class Volvo extends Car {
public function intro() : string {
return "Proud to be Swedish! I'm a $this->name!";
}
}
class Citroen extends Car {
public function intro() : string {
return "French extravagance! I'm a $this->name!";
}
}
// Create objects from the child classes
$audi = new audi("Audi");
echo $audi->intro();
echo "<br>";
$volvo = new volvo("Volvo");
echo $volvo->intro();
echo "<br>";
$citroen = new citroen("Citroen");
echo $citroen->intro();
?>
Interface
Interfaces allow you to specify what methods a class should implement.
<?php
interface Animal {
public function makeSound();
}
class Cat implements Animal {
public function makeSound() {
echo "Meow";
}
}
$animal = new Cat();
$animal->makeSound();
?>
Interfaces vs. Abstract Classes
Interface are similar to abstract classes. The difference between interfaces and abstract classes are:
Interfaces cannot have properties, while abstract classes can
All interface methods must be public, while abstract class methods is public or protected
All methods in an interface are abstract, so they cannot be implemented in code and the abstract keyword is not necessary
Classes can implement an interface while inheriting from another class at the same time
Traits
PHP only supports single inheritance: a child class can inherit only from one single parent. Traits are used to declare methods that can be used in multiple classes.
<?php
trait message1 {
public function msg1() {
echo "OOP is fun! ";
}
}
class Welcome {
use message1;
}
$obj = new Welcome();
$obj->msg1();
Static Methods
Static methods can be called directly - without creating an instance of the class first.
<?php
class ClassName {
public static function staticMethod() {
echo "Hello World!";
}
}
?>
ClassName::staticMethod();
Static Properties
Static properties can be called directly - without creating an instance of a class.
<?php
class ClassName {
public static $staticProp = "W3Schools";
}
?>
ClassName::$staticProp;
Source: https://www.w3schools.com/php/php_oop_what_is.asp