Skip to content

Instantly share code, notes, and snippets.

@dbalabka
Created May 19, 2017 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbalabka/ccaff02f6c0fed5a4d0f73514a0c002e to your computer and use it in GitHub Desktop.
Save dbalabka/ccaff02f6c0fed5a4d0f73514a0c002e to your computer and use it in GitHub Desktop.
Using new PHP7 operator "..." to provide variable number of arguments into constructor we can easier manage child classes constructor interfaces.
<?php
class ParentClass
{
public $a;
public $b;
public function __construct($a, $b)
{
$this->a = $a;
$this->b = $b;
}
}
class ChildClass extends ParentClass
{
public $c;
/**
* Using ... we can add any amount of arguments in ParentClass constructor
* w/o breaking ChildClass constructor interface.
*/
public function __construct($c, ...$parentArgs)
{
parent::__construct(...$parentArgs);
$this->c = $c;
}
}
list($a, $b, $c) = ['a', 'b', 'c'];
$obj = new ChildClass($a, $b, $c);
var_dump($obj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment