Skip to content

Commit

Permalink
#272 - Example of ghost object skipped properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Jan 28, 2016
1 parent 6bf00cf commit 65358ec
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions examples/ghost-object-skipped-properties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

use ProxyManager\Factory\LazyLoadingGhostFactory;
use ProxyManager\Proxy\GhostObjectInterface;

class User
{
private $id;
private $username;

public function getId() : int
{
return $this->id;
}

public function getUsername() : string
{
return $this->username;
}
}

/* @var $proxy User */
$proxy = (new LazyLoadingGhostFactory())->createProxy(
User::class,
function (GhostObjectInterface $proxy, string $method, array $parameters, & $initializer, array $properties) {
$initializer = null;

var_dump('Triggered lazy-loading!');

$properties["\0User\0username"] = 'Ocramius';

return true;
},
[
'skippedProperties' => [
"\0User\0id",
],
]
);

$idReflection = new \ReflectionProperty(User::class, 'id');

$idReflection->setAccessible(true);
$idReflection->setValue($proxy, 123);

var_dump($proxy->getId());
var_dump($proxy->getUsername());

0 comments on commit 65358ec

Please sign in to comment.