Skip to content

Instantly share code, notes, and snippets.

@mxr576
Last active September 12, 2017 14:22
Show Gist options
  • Save mxr576/541304c594d126792b5d to your computer and use it in GitHub Desktop.
Save mxr576/541304c594d126792b5d to your computer and use it in GitHub Desktop.
If you don't know the difference in PHP between the self and the static keywords, then here is a dummy example for you. https://3v4l.org/HBXAe
<?php
class Vehicle {
/**
* @var string
*/
private $name;
protected static $type = 'vehicle';
/**
* Vehicle constructor.
*
* @param $name
*/
public function __construct($name) {
$this->name = $name;
}
/**
* @param $type
* @return static
*/
public static function createStatic($type) {
return new static($type);
}
/**
* @param $type
* @return \Vehicle
*/
public static function createSelf($type) {
return new self($type);
}
/**
* @return string
*/
public function getStaticType() {
return static::$type;
}
/**
* @return string
*/
public function getSelfType() {
return self::$type;
}
}
class Bicycle extends Vehicle {
protected static $type = 'bicycle';
/**getSelfType
* @inheritDoc
*/
public function __construct($name) {
parent::__construct($name);
}
}
class Car extends Vehicle {
protected static $type = 'car';
/**
* @inheritDoc
*/
public function __construct($name) {
parent::__construct($name);
}
}
$vehicle = new Vehicle('vehicle1');
$bicycle = new Bicycle('bicycle1');
$car = new Car('car1');
var_dump('new Vehicle(\'vehicle\') is a ' . get_class($vehicle));
var_dump('new Bicycle(\'bicycle\') is a ' . get_class($bicycle));
var_dump('new Car(\'car\') is a ' . get_class($car));
var_dump('$car->getSelfType() is ' . $car->getSelfType());
var_dump('$car->getStaticType() is ' . $car->getStaticType());
$staticThing1 = Vehicle::createStatic('staticThing1');
$staticThing2 = Bicycle::createStatic('staticThing2');
$staticThing3 = Car::createStatic('staticThing3');
var_dump('Vehicle::createStatic(\'staticThing1\') is a ' . get_class($staticThing1));
var_dump('Bicycle::createStatic(\'staticThing2\') is a ' . get_class($staticThing2));
var_dump('Car::createStatic(\'staticThing3\') is a ' . get_class($staticThing3));
var_dump('$staticThing3->getSelfType() is ' . $staticThing3->getSelfType());
var_dump('$staticThing3->getStaticType() is ' . $staticThing3->getStaticType());
$selfThing1 = Vehicle::createSelf('selfThing1');
$selfThing2 = Bicycle::createSelf('selfThing2');
$selfThing3 = Car::createSelf('selfThing3');
var_dump('Vehicle::createSelf(\'selfThing1\') is a ' . get_class($selfThing1));
var_dump('Bicycle::createSelf(\'selfThing2\') is a ' . get_class($selfThing2));
var_dump('Car::createSelf(\'selfThing3\') is a ' . get_class($selfThing3));
var_dump('$selfThing3->getSelfType() is ' . $selfThing3->getSelfType());
var_dump('$selfThing3->getStaticType() is ' . $selfThing3->getStaticType());
new Vehicle('vehicle') is a Vehicle
new Bicycle('bicycle') is a Bicycle
new Car('car') is a Car
$car->getSelfType() is vehicle
$car->getStaticType() is car
Vehicle::createStatic('statisThing1') is a Vehicle
Bicycle::createStatic('statisThing2') is a Bicycle
Car::createStatic('statisThing3') is a Car
$staticThing3->getSelfType() is vehicle
$staticThing3->getStaticType() is car
Vehicle::createSelf('selfThing1') is a Vehicle
Bicycle::createSelf('selfThing2') is a Vehicle
Car::createSelf('selfThing3') is a Vehicle
$selfThing3->getSelfType() is vehicle
$selfThing3->getStaticType() is vehicle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment