At the moment I am working on a parser, comprised of a Document
class that takes input and determines a number of processing steps to be taken. The logic of each step is dynamic to various circumstances, so is handed off to a Processor
class which is passed to the Document
at construction.
The goal is that my use of it will eventually look something like:
$document = new Document($input, Processors\XYZ::class);
$document->parse();
$document->output();
Document->__construct()
also creates and stores in a property a new instance of the passed processor class. This leads to the part I am unsure of: The Processor
instance also needs a link back to the Document
. I am not sure whether I should be passing that by reference or not. I haven't found anything about this particular use of references - http://php.net/manual/en/language.references.pass.php is only really talking about functions, nothing to do with classes or instances.
I threw together this minimal test:
class base {
//function __construct($maker) {
function __construct(&$maker) {
if (!is_a($maker, makeSay::class, true)) { echo 'oh no!<br/>';}
$this->maker = $maker;
}
function say() {echo 'Base, made by ' . $this->maker->name;}
}
class child extends base {
function say() {echo 'Child, made by ' . $this->maker->name;}
}
class makeSay {
public $name = 'default';
function __construct($thing, $name) {
if (!is_a($thing, base::class, true)) { echo 'oh no!<br/>';}
$this->thing = new $thing($this);
$this->name = $name;
}
function say() {$this->thing->say();}
}
$a = new makeSay(child::class, 'This Guy');
$a->say();
... which has similar logic to the constructors in my real script, but it seems as though there is no difference in execution whether base->__construct()
looks like function __construct(&$maker)
or function __construct($maker)
.
Is there any difference to be expected between the two?