Skip to content

Instantly share code, notes, and snippets.

@juzna
Created May 7, 2012 19:45
Show Gist options
  • Save juzna/2629922 to your computer and use it in GitHub Desktop.
Save juzna/2629922 to your computer and use it in GitHub Desktop.
PHP experiment: object construction
<?php
/**
* Experiment: constructor to have already partially initialized object, before it gets called.
*/
class Foobar {
private $a;
private $b;
private $c;
public function __construct($a) {
$this->a = $a;
var_dump($this->a, $this->b, $this->c);
}
public function setA($a) {
$this->a = $a;
}
public function setB($b) {
$this->b = $b;
}
public function setC($c) {
$this->c = $c;
}
}
// First experiment - normal construct
$f1 = new Foobar('A1'); // dumps A1, NULL, NULL
echo "----------------------------\n";
// Second experiment - constructor with partially initialized object
$rc = new ReflectionClass('Foobar');
$f2 = $rc->newInstanceWithoutConstructor();
$f2->setB('B2');
$f2->__construct('A2'); // dumps A2, B2, NULL
echo "----------------------------\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment