PHP code example of dgame / php-object

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 Exception());
foreach (['message', 'code', 'file', 'line'] as $name) {
    $property = $facade->getPropertyByName($name);

    $this->assertNotNull($property);
    $this->assertEquals($name, $property->getName());
    $this->assertNotEquals(0, ReflectionProperty::IS_PROTECTED & $property->getModifiers());
}

$facade = new ObjectFacade(new Exception());
$method = $facade->getMethodByName('getMessage');

$this->assertNotNull($method);
$this->assertEquals('getMessage', $method->getName());
$this->assertNotEquals(0, (ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_FINAL) & $method->getModifiers());

$facade = new ObjectFacade(new Exception());
$method = $facade->getGetterMethod('message');

$this->assertNotNull($method);
$this->assertEquals('getMessage', $method->getName());

$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'));

$facade = new ObjectFacade(
    new class() {
        public $foo = 42;
    }
);

$this->assertEquals(42, $facade->getValueByProperty('foo'));
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo'));
$facade->setValueByProperty('foo', 23);
$this->assertEquals(23, $facade->getValueByProperty('foo'));
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo'));
$facade->setValueByProperty('foo', null);
$this->assertNull($facade->getValueByProperty('foo'));
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo'));
$facade->setValueByProperty('foo', 1337);
$this->assertEquals(1337, $facade->getValueByProperty('foo'));
$this->assertEquals($facade->getObject()->foo, $facade->getValueByProperty('foo'));

$facade = new ObjectFacade(
    new class() {
        public $foo = 42;
    }
);

$facade->setValue('foo', 3537);
$this->assertEquals(3537, $facade->getValue('foo'));

new class() {
    private $foo = 42;

    public function setFoo(int $foo)
    {
        $this->foo = $foo;
    }
}

$facade->setValue('foo', 3537);
$this->assertEquals(3537, $facade->getValue('foo'));