Nette\ComponentModel\Component::setParent PHP Method

setParent() public method

Sets the parent of this component. This method is managed by containers and should not be called by applications
public setParent ( Nette\ComponentModel\IContainer $parent = NULL, $name = NULL ) : self
$parent Nette\ComponentModel\IContainer
return self
    public function setParent(IContainer $parent = NULL, $name = NULL)
    {
        if ($parent === NULL && $this->parent === NULL && $name !== NULL) {
            $this->name = $name;
            // just rename
            return $this;
        } elseif ($parent === $this->parent && $name === NULL) {
            return $this;
            // nothing to do
        }
        // A component cannot be given a parent if it already has a parent.
        if ($this->parent !== NULL && $parent !== NULL) {
            throw new Nette\InvalidStateException("Component '{$this->name}' already has a parent.");
        }
        // remove from parent?
        if ($parent === NULL) {
            $this->refreshMonitors(0);
            $this->parent = NULL;
        } else {
            // add to parent
            $this->validateParent($parent);
            $this->parent = $parent;
            if ($name !== NULL) {
                $this->name = $name;
            }
            $tmp = [];
            $this->refreshMonitors(0, $tmp);
        }
        return $this;
    }