PLZ: A handy PHP library for lazy programmers
  • Namespace
  • Class
  • Tree
  • Todo

Namespaces

  • amekusa
    • plz
  • PHP

Classes

  • alt
  • arr
  • constant
  • dom
  • fn
  • num
  • obj
  • op
  • path
  • str
  • sys
  • T
  • type
  • xml

Exceptions

  • ErrorException

Class obj

Object utilities

To get started, place the following line around top of your code.

use amekusa\plz\obj;
Abstract
Namespace: amekusa\plz
Located at obj.php

Methods summary

public static boolean
# eq( object $X, object $Y )

Returns whether two objects are equal

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)

Parameters

$X
An object to compare with $Y
$Y
An object to compare with $X

Returns

boolean

Example

Comparing objects of the same class

$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.';
Does $P1 equal to $P2? - No.
Does $P1 equal to $P3? - Yes.

equals() evaluation

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.';
Does $X equal to $Y? - Yes.

public static
# get( object $X, string $Prop, mixed $Alt = null )

Returns a property of an object

Returns a property of an object

If $X has a getter method, calls it.

Parameters

$X
An object to retrieve a property from
$Prop
The name of a property to get
$Alt
(optional) A fail-safe value

Example

Demonstration

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
string(5) "Alice"
NULL
string(7) "Secret!"

Calling a getter method

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
string(5) "Alice"
int(21)
int(21)

PLZ: A handy PHP library for lazy programmers API documentation generated by ApiGen