Last active
August 29, 2015 14:05
-
-
Save JVMartin/10932980508fd0cc99c3 to your computer and use it in GitHub Desktop.
How to call a parent's constructor if it exists, or gracefully continue if not.
This file contains 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 Foo extends Mystery { | |
public function __construct() | |
{ | |
$reflection = new ReflectionClass(__CLASS__); | |
if (method_exists($reflection->getParentClass()->name, '__construct')) { | |
parent::__construct(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment