Last active
May 15, 2016 12:33
-
-
Save MacDada/4527f591f0f8437b804e1d3cd24bd0be to your computer and use it in GitHub Desktop.
PHP OOP reuse with composition, inheritance and traits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BasicStuff | |
{ | |
public function foo() | |
{ | |
/* does basic stuff */ | |
} | |
} | |
class BasicAndExtraStuff | |
{ | |
private $basicStuff; | |
public function __construct(BasicStuff $basicStuff) | |
{ | |
$this->basicStuff = $basicStuff; | |
} | |
public function foo() | |
{ | |
$this->basicStuff->foo(); | |
/* do something extra */ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BasicStuff | |
{ | |
public function foo() | |
{ | |
/* does basic stuff */ | |
} | |
} | |
class BasicAndExtraStuff extends BasicStuff | |
{ | |
public function foo() | |
{ | |
parent::foo(); | |
/* do something extra */ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BasicStuff | |
{ | |
public static function foo() | |
{ | |
/* does basic stuff */ | |
} | |
} | |
class BasicAndExtraStuff | |
{ | |
public function foo() | |
{ | |
BasicStuff::foo(); | |
/* do something extra */ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait BasicStuff | |
{ | |
public function foo() | |
{ | |
/* does basic stuff */ | |
} | |
} | |
class BasicAndExtraStuff | |
{ | |
use BasicStuff { foo as basicFoo; } | |
public function foo() | |
{ | |
$this->basicFoo(); | |
/* do something extra */ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment