Skip to content

Instantly share code, notes, and snippets.

@JVMartin
Last active August 29, 2015 14:05
Show Gist options
  • Save JVMartin/10932980508fd0cc99c3 to your computer and use it in GitHub Desktop.
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.
<?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