Skip to content

Instantly share code, notes, and snippets.

@acapps
Created March 31, 2017 22:54
Show Gist options
  • Save acapps/dd55d59a46966a8f71f34dd920863158 to your computer and use it in GitHub Desktop.
Save acapps/dd55d59a46966a8f71f34dd920863158 to your computer and use it in GitHub Desktop.
<?php
/**
* Class Base
*/
Class Base {
/**
* @param $name
* @return string
*/
public static function hello($name) {
return sprintf('Hello, %s!', $name);
}
}
/**
* Class Extended
*/
Class Extended extends Base {
/**
* @param $name string
* @return string
*/
public static function hello($name = null) {
if (null === $name) {
$name = 'World';
}
return parent::hello($name);
}
}
/**
* Class ExtendedLegacy
*/
Class ExtendedLegacy extends Base {
/**
* @param $name string
* @return string
*/
public static function hello($name = null) {
if (null === $name) {
$name = 'Universe';
}
return sprintf('Hello, %s!', ucwords($name));
}
}
$emptyName = null;
$name = 'bob';
echo '=== Base ===' . PHP_EOL;
echo Base::hello($emptyName) . PHP_EOL;
echo Base::hello($name) . PHP_EOL;
echo '=== Extended ===' . PHP_EOL;
echo Extended::hello($emptyName) . PHP_EOL;
echo Extended::hello($name) . PHP_EOL;
echo '=== ExtendedLegacy ===' . PHP_EOL;
echo ExtendedLegacy::hello($emptyName) . PHP_EOL;
echo ExtendedLegacy::hello($name) . PHP_EOL;
@acapps
Copy link
Author

acapps commented Mar 31, 2017

The following example outputs:

=== Base ===
Hello, !
Hello, bob!
=== Extended ===
Hello, World!
Hello, bob!
=== ExtendedLegacy ===
Hello, Universe!
Hello, Bob!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment