1. Go to this page and download the library: Download dgame/php-object 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/ */
dgame / php-object example snippets
$facade = new ObjectFacade(new Exception());
$this->assertTrue($facade->hasMethod('getMessage'));
$this->assertFalse($facade->hasMethod('foo'));
$facade = new ObjectFacade(new Exception());
foreach (['message', 'code', 'file', 'line'] as $property) {
$this->assertTrue($facade->hasProperty($property));
}
$this->assertFalse($facade->hasProperty('foo'));
$facade = new ObjectFacade(
new class() {
public function setFoo()
{
}
}
);
$method = $facade->getSetterMethod('foo');
$this->assertNotNull($method);
$this->assertEquals('setFoo', $method->getName());
$exception = new Exception('Test');
$facade = new ObjectFacade($exception);
$this->assertEquals($exception, $facade->getObject());
$this->assertEquals('Test', $facade->getValueByMethod('message'));
$this->assertEquals($exception->getMessage(), $facade->getValueByMethod('message'));
$this->assertEquals($exception->getFile(), $facade->getValueByMethod('file'));
$this->assertEquals($exception->getLine(), $facade->getValueByMethod('line'));
$this->assertNull($facade->getValueByMethod('unknown'));
$facade = new ObjectFacade(
new class() {
public $foo = 42;
public $bar = Exception::class;
}
);
$this->assertEquals(42, $facade->getValueByProperty('foo'));
$this->assertEquals(Exception::class, $facade->getValueByProperty('bar'));
$this->assertNull($facade->getValueByProperty('unknown'));
$facade = new ObjectFacade(new Exception());
$this->assertNull($facade->getValueByProperty('line')); // not a public property
$this->assertNull($facade->getValueByProperty('file')); // not a public property
$facade = new ObjectFacade(
new class() {
private $foo = 42;
private $bar;
public function setFoo(int $foo)
{
$this->foo = $foo;
}
public function setFooBar()
{
$this->foo = 1;
$this->bar = 2;
}
public function getFoo(): int
{
return $this->foo;
}
public function setBar(int $bar = null)
{
$this->bar = $bar;
}
public function getBar()
{
return $this->bar;
}
}
);
$this->assertEquals(42, $facade->getValueByMethod('foo'));
$facade->setValueByMethod('foo', 23);
$this->assertEquals(23, $facade->getValueByMethod('foo'));
$facade->setValueByMethod('foo', null); // "setFoo" does not accept null => keep the old value
$this->assertEquals(23, $facade->getValueByMethod('foo'));
$facade->setValueByMethod('foo', 'abc'); // "setFoo" does not accept a string => keep the old value
$this->assertEquals(23, $facade->getValueByMethod('foo'));
$this->assertNull($facade->getValueByMethod('bar'));
$facade->setValueByMethod('bar', 1337);
$this->assertEquals(1337, $facade->getValueByMethod('bar'));
$facade->setValueByMethod('bar', null);
$this->assertNull($facade->getValueByMethod('bar'));
$facade->setValueByMethod('foobar', uniqid());
$this->assertEquals(1, $facade->getValueByMethod('foo'));
$this->assertEquals(2, $facade->getValueByMethod('bar'));