Skip to content

Instantly share code, notes, and snippets.

@skrajewski
Created November 10, 2016 21:42
Show Gist options
  • Save skrajewski/5b8065c67f216647be4a6e9ad8181c82 to your computer and use it in GitHub Desktop.
Save skrajewski/5b8065c67f216647be4a6e9ad8181c82 to your computer and use it in GitHub Desktop.
Create instance of me!
<?php
class Dog {
private $dog;
public function __construct(Dog $dog)
{
$this->dog = $dog;
}
public function getDog()
{
return $this->dog;
}
}
$reflection = new ReflectionClass(Dog::class);
$dog = $reflection->newInstanceWithoutConstructor();
$property = $reflection->getProperty('dog');
$property->setAccessible(true);
$property->setValue($dog, $dog);
var_dump($dog);
var_dump($dog->getDog());
var_dump($dog === $dog);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment