Jump to content

Reason for injecting dependencies using $this->wire($object)


abdus
 Share

Recommended Posts

I'm building a module using Comments field as reference. Inside Comments module and many others, there's always this expression which is used to inject dependencies (according to Wire class documentation)

$fieldset = $this->wire(new InputfieldWrapper());

Is there a reason for injecting API variables inside objects? Because everything seems to work fine without.

2017-04-30_22-30-23.thumb.gif.b15fa24f92e97f4fdc0c85e7a02ed9ea.gif

  • Like 1
Link to comment
Share on other sites

Not 100% sure on this, but I think it will be because of the multi-instance support introduced in PW3.

The example you gave from CommentFilterAkismet.module...

$inputfields = $this->wire(new InputfieldWrapper());

...if you check the PW2.x version of this file, before multi-instance support was added, it is simply...

$inputfields = new InputfieldWrapper();

 

  • Like 3
Link to comment
Share on other sites

17 minutes ago, LostKobrakai said:

Exactly this. 

That makes it clear. I've also found this part from the core inside Wire class.

<?php
// /wire/core/Wire.php
public function wire($name = '', $value = null, $lock = false) {
    // ...
  
    if(is_object($name)) {
        // make an object wired (inject ProcessWire instance to object)
        if($name instanceof WireFuelable) {
            if($this->_wire) $name->setWire($wire); // inject fuel, PW 3.0 
            if(is_string($value) && $value) {
                // set as new API var if API var name specified in $value
                $wire->fuel()->set($value, $name, $lock);
            }
            $value = $name; // return the provided instance
        } else {
            throw new WireException("Wire::wire(\$o) expected WireFuelable for \$o and was given " . get_class($name));
        }
    }
  
    // ...
}

Here, setWire() method sets the instance.

	/**
	 * Set the current ProcessWire instance for this object (PW 3.0)
	 * 
	 * Specify no arguments to get, or specify a ProcessWire instance to set.
	 * 
	 * #pw-internal
	 *
	 * @param ProcessWire $wire
	 *
	 */
	public function setWire(ProcessWire $wire) {
		$this->_wire = $wire;
		$this->getInstanceNum();
	}

Another question @LostKobrakai: Does every single object from PW core needs an instance to be set? What classes are instance-critical, so to speak?

Link to comment
Share on other sites

If the class does use `$this->wire()` somewhere it's most-likely dependent on some state of the actual processwire instance, so the class does need to be wired after construction. I mean the config, installed modules, available pages and so on are all potentially different in different processwire instances if running in multi-instance mode.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...