Skip to content

Instantly share code, notes, and snippets.

@aldehir
Created October 10, 2012 00:52
Show Gist options
  • Select an option

  • Save aldehir/3862483 to your computer and use it in GitHub Desktop.

Select an option

Save aldehir/3862483 to your computer and use it in GitHub Desktop.
Ulti's exercise
<?php
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
function assert_handler($f, $l, $c) {
echo "Assert failed at :$f:$l: $c";
echo "\n<br />";
exit();
}
assert_options(ASSERT_CALLBACK, "assert_handler");
// --------- Your code starts here ----------------
abstract class Animal {
abstract public function sound();
}
class Dog extends Animal {
public function sound() {
return "woof";
}
}
class Cat extends Animal {
public function sound() {
return "meow";
}
}
// --------- Your code ends here ------------------
$dog = &new Dog;
$cat = &new Cat;
// Animal must have method sound()
assert(method_exists(Animal, "sound"));
// Dog must be a subclass of Animal
assert($dog instanceof Animal);
// Cat must be a subclass of Animal
assert($cat instanceof Animal);
// Dog cannot be a subclass of Cat
assert(!(Dog instanceof Cat));
// Cat cannot be a subclass of Dog
assert(!(Cat instanceof Dog));
// Dog must make the sound 'woof'
assert($dog->sound() == "woof");
// Cat must make the sound 'meow'
assert($cat->sound() == "meow");
echo 'Success!';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment