1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
<?php namespace amekusa\plz; main::required;
/**
* Object utilities
*
* To get started, place the following line around top of your code.
* ```php
* use amekusa\plz\obj;
* ```
*/
abstract class obj {
/**
* Returns whether two objects are equal
*
* If the objects are **same instance**, returns `true`.
*
* Otherwise the result is same as `$X == $Y`
* except for under certain conditions:
*
* + If `$X` has `equals()` method, invokes `$X->equals($Y)`
* + If `$Y` has `equals()` method, invokes `$Y->equals($X)`
*
* @example Comparing objects of the same class
* ```php
* $P1 = new stdClass();
* $P1->x = 1.0;
* $P1->y = 2.0;
*
* $P2 = new stdClass();
* $P2->x = 2.0;
* $P2->y = 4.0;
*
* $P3 = new stdClass();
* $P3->x = 1.0;
* $P3->y = 2.0;
*
* echo 'Does $P1 equal to $P2? - ';
* echo obj::eq($P1, $P2) ? 'Yes.' : 'No.';
* echo "\n";
* echo 'Does $P1 equal to $P3? - ';
* echo obj::eq($P1, $P3) ? 'Yes.' : 'No.';
* ```
* ```php
* Does $P1 equal to $P2? - No.
* Does $P1 equal to $P3? - Yes.
* ```
* @example `equals()` evaluation
* ```php
* class Fraction {
* public $numerator, $denominator;
*
* function __construct($Numerator, $Denominator) {
* $this->numerator = $Numerator;
* $this->denominator = $Denominator;
* }
*
* function quotient() {
* return $this->numerator / $this->denominator;
* }
*
* function equals($Fraction) {
* return $this->quotient() == $Fraction->quotient();
* }
* }
*
* $X = new Fraction(2, 1); // = 2/1
* $Y = new Fraction(4, 2); // = 4/2
*
* echo 'Does $X equal to $Y? - ';
* echo obj::eq($X, $Y) ? 'Yes.' : 'No.';
* ```
* ```php
* Does $X equal to $Y? - Yes.
* ```
* @param object $X An object to compare with `$Y`
* @param object $Y An object to compare with `$X`
* @return boolean
*/
static function eq($X, $Y) {
if ($X === $Y) return true; // Same instance
if (is_callable(array ($X, 'equals'))) return $X->equals($Y);
if (is_callable(array ($Y, 'equals'))) return $Y->equals($X);
return $X == $Y;
}
/**
* Returns a property of an object
*
* If `$X` has a *getter* method, calls it.
*
* @example Demonstration
* ```php
* class Student {
* public $name;
* private $age;
*
* public function __construct($Name, $Age) {
* $this->name = $Name;
* $this->age = $Age;
* }
* }
*
* $student = new Student('Alice', 21);
* var_dump( obj::get($student, 'name') );
* // Because of $age is private and no getter method, you can never get it
* var_dump( obj::get($student, 'age') ); // Alternates with NULL
* var_dump( obj::get($student, 'age', 'Secret!') ); // Alternates with a string
* ```
* ```php
* string(5) "Alice"
* NULL
* string(7) "Secret!"
* ```
* @example Calling a *getter* method
* ```php
* class Student {
* public $name;
* private $age;
*
* public function __construct($Name, $Age) {
* $this->name = $Name;
* $this->age = $Age;
* }
*
* // The getter method for $age
* public function getAge() {
* return $this->age;
* }
* }
*
* $student = new Student('Alice', 21);
* var_dump( obj::get($student, 'name') );
* var_dump( obj::get($student, 'age') ); // Invokes $student->getAge()
* var_dump( obj::get($student, 'age', 'Secret!') ); // No alternation
* ```
* ```php
* string(5) "Alice"
* int(21)
* int(21)
* ```
* @param object $X An object to retrieve a property from
* @param string $Prop The name of a property to get
* @param mixed $Alt *(optional)* A fail-safe value
*/
static function get($X, $Prop, $Alt = null) {
$getter = array ($X, 'get' . ucfirst($Prop));
if (is_callable($getter)) return call_user_func($getter);
$props = get_object_vars($X);
if (!array_key_exists($Prop, $props)) return $Alt;
return $props[$Prop];
}
/**
* @ignore
* @todo Implement
* @param unknown $Prop
* @param unknown $X
*/
static function set($Prop, $X) {
}
}