|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Multi Inheritance |
| 4 | + * |
| 5 | + * @desc |
| 6 | + * In some languages you can extend multiple classes, for example python: |
| 7 | + * class User(Address, Profile): |
| 8 | + * |
| 9 | + * In PHP we accomplish this with Traits, they are very convenient. |
| 10 | + * These were introduced in 5.4. |
| 11 | + * |
| 12 | + * @usage |
| 13 | + * |
| 14 | + * Give any class re-usable functionality by typing it once. |
| 15 | + * |
| 16 | + * @example |
| 17 | + * We will have a User class that extends a fake Model |
| 18 | + * We want the User to inherit features from: Profile, TimeInfo |
| 19 | + * We will use Traits |
| 20 | + * |
| 21 | + */ |
| 22 | +require_once '../constants.php'; // For NEWLINE output |
| 23 | + |
| 24 | +// -------------------------------------------------------- |
| 25 | +// One way of doing this is by faking it |
| 26 | +// Note: This isn't considered bad, it's a pattern that |
| 27 | +// actually separates dependencies and can be used |
| 28 | +// for many things other than this. |
| 29 | +// -------------------------------------------------------- |
| 30 | + |
| 31 | +class BasicModel {}; // Our main item to Extend |
| 32 | +class BasicProfile {}; // A utility class |
| 33 | +class BasicTimeInfo {}; // A utility class |
| 34 | + |
| 35 | +// You'd do something like this |
| 36 | +class BasicUser extends BasicModel { |
| 37 | + public function __construct() { |
| 38 | + $this->profile = new BasicProfile(); |
| 39 | + $this->dateTime = new BasicTimeInfo(); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +// -------------------------------------------------------- |
| 45 | +// A Better way to do it, use Traits (They are Classes) |
| 46 | +// -------------------------------------------------------- |
| 47 | +class Model { |
| 48 | + public $info = 'I something important, I think.'; |
| 49 | +}; |
| 50 | + |
| 51 | +trait Profile { |
| 52 | + public function calculateAge($birthday) { |
| 53 | + // I'm not doing logic :) |
| 54 | + return 90; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +trait TimeInfo { |
| 59 | + public function getUTC() { |
| 60 | + // Not doing logic :) |
| 61 | + return time(); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class User extends Model { |
| 66 | + use Profile; |
| 67 | + use TimeInfo; |
| 68 | + |
| 69 | + public function hello() { |
| 70 | + return 'hello'; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// -------------------------------------------------------- |
| 75 | +// Example |
| 76 | +// -------------------------------------------------------- |
| 77 | + |
| 78 | +$user = new User; |
| 79 | + |
| 80 | +$a = $user->hello(); |
| 81 | +echo "From the user Class: $a" . NEWLINE; |
| 82 | + |
| 83 | +$b = $user->info; |
| 84 | +echo "From the model Class: $b" . NEWLINE; |
| 85 | + |
| 86 | +$c = $user->calculateAge('10-25-84'); |
| 87 | +echo "From the Profile Trait: $c" . NEWLINE; |
| 88 | + |
| 89 | +$d = $user->getUTC(); |
| 90 | +echo "From the TimeInfo Trait: $b" . NEWLINE; |
| 91 | + |
| 92 | +echo NEWLINE; |
0 commit comments