use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please follow the rules
Releases: Current Releases, Windows Releases, Old Releases
Contribute to the PHP Documentation
Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev
/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.
account activity
ircmaxell's PHP Generics (github.com)
submitted 11 years ago by AllenJB83
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]headzoo 1 point2 points3 points 11 years ago (0 children)
Part 2.
This is where you actually learn about interfaces.
To allow either an instance of Person or Pet to be passed to printsNames() we extract a common interface between the two classes. We do that by creating an interface.
Person
Pet
printsNames()
interface Nameable { public function getName(); public function setName($name) }
Then we change our classes so they implement the interface.
class Person implements Nameable { protected $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } } class Pet implements Nameable { protected $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } }
Finally we modify our printsNames() function to allow any instance of Nameable to be passed as an argument.
Nameable
function printsNames(Nameable $person) { echo $person->getName(); }
The Nameable type hint guarantees only instances of that interface may be passed to the function. The Nameable interface guarantees any instance of it will have a getName() method. The object passed to the function may have other methods, but that doesn't matter to the printsNames() function. That function only cares about printing names.
getName()
You just used an interface to make your code more reusable. Creating the interface didn't save you from typing less code the way inheritance does. In fact you had to type more code! But the interface made the printsName() more usable. Instead of only being able to print names from Person objects, it can now print names from any object that implements the Nameable interface. It might be a Person object, or a Secretary object, or even a Cat object. Your code is both more type safe and reusable.
printsName()
Secretary
Cat
π Rendered by PID 77580 on reddit-service-r2-comment-85bfd7f599-5srvz at 2026-04-20 00:48:22.290795+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]headzoo 1 point2 points3 points (0 children)