note 115411 added to reflectionclass.newinstance

From: Date: Sat, 19 Jul 2014 19:43:05 +0000
Subject: note 115411 added to reflectionclass.newinstance
Groups: php.notes 
Request: Send a blank email to php-notes+get-199979@lists.php.net to get a copy of this message
I sometimes uses it in abstract factory pattern. Defined a default class in file .ini. A simple example below: //file A.php Class A { private $text; public function __construct($text) { $this->text = $text; } public print() { echo $this->foo, PHP_EOL; } } //file Factory.php class Factory { private $Class = 'A'; //This variable could be dynamic. public function __construct() { } public function make( $param ) { $reflector = new reflectionClass( $this->Class ); return $reflector->newInstanceArgs( $param ); } } $factory = new Factory(); $B = $factory->make("Good"); $C = $factory->make("Bad"); $B->print(); //Good $C->print(); //Bad ---- Server IP: 72.52.91.14 Probable Submitter: 179.236.66.156 ---- Manual Page -- http://php.net/manual/en/reflectionclass.newinstance.php Edit -- https://master.php.net/note/edit/115411 Del: integrated -- https://master.php.net/note/delete/115411/integrated Del: useless -- https://master.php.net/note/delete/115411/useless Del: bad code -- https://master.php.net/note/delete/115411/bad+code Del: spam -- https://master.php.net/note/delete/115411/spam Del: non-english -- https://master.php.net/note/delete/115411/non-english Del: in docs -- https://master.php.net/note/delete/115411/in+docs Del: other reasons-- https://master.php.net/note/delete/115411 Reject -- https://master.php.net/note/reject/115411 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199979) next »