Created
May 7, 2012 19:45
-
-
Save juzna/2629922 to your computer and use it in GitHub Desktop.
PHP experiment: object construction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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