Skip to content

Instantly share code, notes, and snippets.

@bagart
Last active January 31, 2018 09:51
Show Gist options
  • Save bagart/2bab7c4db81a51d0a6c361de3d3f89c0 to your computer and use it in GitHub Desktop.
Save bagart/2bab7c4db81a51d0a6c361de3d3f89c0 to your computer and use it in GitHub Desktop.
private access in PHP / encapsulation hack
<?php
declare(strict_types=1);
final class A {
private $prop;
public function __construct(DateTime $prop) { $this->prop = $prop; }
public function getProp() { return $this->prop; }
}
$a = new A(new DateTime());
var_dump($a->getProp());
(function (){
return $this->prop = "injection success";
})->call($a);
var_dump($a->getProp());
@bagart
Copy link
Author

bagart commented Jan 31, 2018

RESULT

object(DateTime)#2 (3) {
  ["date"]=>
  string(26) "2018-01-31 01:51:23.457999"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(10) "US/Pacific"
}
string(17) "injection success"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment