1. Go to this page and download the library: Download smoren/type-tools library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Smoren\TypeTools\ObjectTypeCaster;
class ParentClass
{
public int $a;
protected int $b;
public function __construct(int $a, int $b)
{
$this->a = $a;
$this->b = $b;
}
public function toArray(): array
{
return [$this->a, $this->b];
}
}
class ChildClass extends ParentClass
{
private $c = null;
public function __construct(int $a, int $b, int $c)
{
parent::__construct($a, $b);
$this->c = $c;
}
public function toArray(): array
{
return [$this->a, $this->b, $this->c];
}
}
/* Downcast */
$parentClassObject = new ParentClass(1, 2);
print_r($parentClassObject->toArray());
// [1, 2]
$castedToChildClass = ObjectTypeCaster::cast($parentClassObject, ChildClass::class);
print_r($castedToChildClass->toArray());
// [1, 2, null]
var_dump(get_class($castedToChildClass));
// ChildClass
/* Upcast */
$childClassObject = new ChildClass(1, 2, 3);
print_r($childClassObject->toArray());
// [1, 2, 3]
$castedToParentClass = ObjectTypeCaster::cast($childClassObject, ParentClass::class);
print_r($castedToParentClass->toArray());
// [1, 2]
var_dump(get_class($castedToParentClass));
// ParentClass
use Smoren\TypeTools\ObjectAccess;
class MyClass {
public int $publicProperty = 1;
private int $privateProperty = 2;
public function getPrivateProperty(): int
{
return $this->privateProperty;
}
}
$myObject = new MyClass();
// Getting by name:
var_dump(ObjectAccess::getPropertyValue($myObject, 'publicProperty'));
// 1
// Getting by getter (getPrivateProperty()):
var_dump(ObjectAccess::getPropertyValue($myObject, 'privateProperty'));
// 2
use Smoren\TypeTools\ObjectAccess;
class MyClass {
public int $publicProperty = 1;
private int $privateProperty = 2;
public function setPrivateProperty(int $value): void
{
$this->privateProperty = $value;
}
public function toArray(): array
{
return [$this->publicProperty, $this->privateProperty];
}
}
$myObject = new MyClass();
// Setting by name:
ObjectAccess::setPropertyValue($myObject, 'publicProperty', 11);
// Setting by setter (setPrivateProperty()):
ObjectAccess::getPropertyValue($myObject, 'privateProperty', 22);
print_r($myObject->toArray());
// [11, 22]
use Smoren\TypeTools\ObjectAccess;
class MyClass {
public int $publicProperty = 1;
private int $privateProperty = 2;
private int $notAccessibleProperty = 3;
public function getPrivateProperty(): int
{
return $this->privateProperty;
}
}
$myObject = new MyClass();
// Accessible by name:
var_dump(ObjectAccess::hasReadableProperty($myObject, 'publicProperty'));
// true
// Accessible by getter:
var_dump(ObjectAccess::hasReadableProperty($myObject, 'privateProperty'));
// true
// Not accessible:
var_dump(ObjectAccess::hasReadableProperty($myObject, 'notAccessibleProperty'));
// false
use Smoren\TypeTools\ObjectAccess;
class MyClass {
public int $publicProperty = 1;
private int $privateProperty = 2;
private int $notAccessibleProperty = 3;
public function setPrivateProperty(int $value): void
{
$this->privateProperty = $value;
}
}
$myObject = new MyClass();
// Accessible by name:
var_dump(ObjectAccess::hasWritableProperty($myObject, 'publicProperty'));
// true
// Accessible by setter:
var_dump(ObjectAccess::hasWritableProperty($myObject, 'privateProperty'));
// true
// Not accessible:
var_dump(ObjectAccess::hasWritableProperty($myObject, 'notAccessibleProperty'));
// false
use Smoren\TypeTools\ObjectAccess;
class MyClass {
public int $publicProperty = 1;
private int $privateProperty = 2;
}
$myObject = new MyClass();
var_dump(ObjectAccess::hasPublicProperty($myObject, 'publicProperty'));
// true
var_dump(ObjectAccess::hasPublicProperty($myObject, 'privateProperty'));
// false
use Smoren\TypeTools\ObjectAccess;
class MyClass {
public function publicMethod(): int
{
return 1;
}
private function privateMethod(): int
{
return 2;
}
}
$myObject = new MyClass();
var_dump(ObjectAccess::hasPublicMethod($myObject, 'publicMethod'));
// true
var_dump(ObjectAccess::hasPublicMethod($myObject, 'privateMethod'));
// false
use Smoren\TypeTools\ObjectAccess;
class MyClass {
public int $publicProperty = 1;
private int $privateProperty = 2;
}
$myObject = new MyClass();
var_dump(ObjectAccess::hasProperty($myObject, 'publicProperty'));
// true
var_dump(ObjectAccess::hasProperty($myObject, 'privateProperty'));
// true
var_dump(ObjectAccess::hasProperty($myObject, 'anotherProperty'));
// false
use Smoren\TypeTools\ObjectAccess;
class MyClass {
public function publicMethod(): int
{
return 1;
}
private function privateMethod(): int
{
return 2;
}
}
$myObject = new MyClass();
var_dump(ObjectAccess::hasMethod($myObject, 'publicMethod'));
// true
var_dump(ObjectAccess::hasMethod($myObject, 'privateMethod'));
// true
use Smoren\TypeTools\MapAccess;
$array = [
'a' => 1,
];
var_dump(MapAccess::get($array, 'a', 0));
// 1
var_dump(MapAccess::get($array, 'b', 0));
// 0
var_dump(MapAccess::get($array, 'b'));
// null
class MyClass {
public int $publicProperty = 1;
private int $privateProperty = 2;
private int $notAccessibleProperty = 3;
public function getPrivateProperty(): int
{
return $this->privateProperty;
}
}
$myObject = new MyClass();
// Accessible by name:
var_dump(MapAccess::get($myObject, 'publicProperty', 0));
// 1
// Accessible by getter:
var_dump(MapAccess::get($myObject, 'privateProperty'));
// 2
// Not accessible:
var_dump(MapAccess::get($myObject, 'notAccessibleProperty', -1));
// -1
var_dump(MapAccess::get($myObject, 'notAccessibleProperty'));
// null
// Nonexistent:
var_dump(MapAccess::get($myObject, 'nonexistentProperty', -1));
// -1
var_dump(MapAccess::get($myObject, 'nonexistentProperty'));
// null
use Smoren\TypeTools\MapAccess;
$array = [
'a' => 1,
];
MapAccess::set($array, 'a', 11);
MapAccess::set($array, 'b', 22);
print_r($array);
// ['a' => 11, 'b' => 22]
class MyClass {
public int $publicProperty = 1;
private int $privateProperty = 2;
public function setPrivateProperty(int $value): void
{
$this->privateProperty = $value;
}
public function toArray(): array
{
return [$this->publicProperty, $this->privateProperty];
}
}
$myObject = new MyClass();
// Accessible by name:
MapAccess::get($myObject, 'publicProperty', 11);
// Accessible by getter:
MapAccess::get($myObject, 'privateProperty', 22);
print_r($myObject->toArray());
// [11, 22]
use Smoren\TypeTools\MapAccess;
$array = [
'a' => 1,
];
var_dump(MapAccess::exists($array, 'a'));
// true
var_dump(MapAccess::exists($array, 'b'));
// false
class MyClass {
public int $publicProperty = 1;
private int $privateProperty = 2;
private int $notAccessibleProperty = 3;
public function getPrivateProperty(): int
{
return $this->privateProperty;
}
}
$myObject = new MyClass();
// Accessible by name:
var_dump(MapAccess::exists($myObject, 'publicProperty'));
// true
// Accessible by getter:
var_dump(MapAccess::exists($myObject, 'privateProperty'));
// true
// Not accessible:
var_dump(MapAccess::get($myObject, 'notAccessibleProperty'));
// false
// Nonexistent:
var_dump(MapAccess::get($myObject, 'nonexistentProperty', -1));
// false
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.