Shares an already created instance via the container;

@param $reference
@param $instance

@return bool
424432
public function shareInstance($instance, $reference = null)
    {

        if (!is_object($instance)) {
            //@TODO maybe throw an error?
            return false;
        }

        $concrete = get_class($instance);

        //if reference is difference from $concrete add Alias
        if (!empty($reference) && ($reference !== $concrete)) {
            $alias = $reference;
            $this->createAlias($concrete, $alias);
        }

        //just save the instance in container, no need for lambdas;
        return $this->container[$concrete] = $instance;
    }
1157210
public static function instance($instance = null)
	{
		if ($instance === true)
		{
			return static::$_instances;
		}
		elseif ($instance !== null)
		{
			if ( ! array_key_exists($instance, static::$_instances))
			{
				return false;
			}

			return static::$_instances[$instance];
		}

		return static::$_instance;
	}
831889
public static function &getInstance( $instance = null )
	{
		if ( is_null( self::$instance ) )
		{
			self::$instance = $instance ? $instance : new self();
			// if ( $callback ) $callback( self::$instance );
		}
		return self::$instance;
	}
781680
public function replicate()
	{
		$attributes = array_except($this->attributes, array($this->getKeyName()));

		with($instance = new static)->setRawAttributes($attributes);

		return $instance->setRelations($this->relations);
	}
1099234
public static function bindInstance($instance, $methodName, $args = null)
    {
        $helper = new self();
        return $helper->createClosure($methodName, $args)->bindTo($instance, get_class($instance));
    }
452960
public function can_hide_show_instance($instance) {
        $context = context_course::instance($instance->courseid);
        if (!has_capability('enrol/guest:config', $context)) {
            return false;
        }

        // If the instance is currently disabled, before it can be enabled, we must check whether the password meets the
        // password policies.
        if ($instance->status == ENROL_INSTANCE_DISABLED) {
            if ($this->get_config('requirepassword')) {
                if (empty($instance->password)) {
                    return false;
                }
            }

            // Only check the password if it is set.
            if (!empty($instance->password) && $this->get_config('usepasswordpolicy')) {
                if (!check_password_policy($instance->password, $errmsg)) {
                    return false;
                }
            }
        }

        return true;
    }
537001
public function addReference(ChildReference $reference)
    {
        if ($this->collReferences === null) {
            $this->initReferences();
        }

        if (!$this->getReferences()->contains($reference)) {
            // only add it if the **same** object is not already associated
            $this->collReferences->push($reference);
            $this->doAddReference($reference);
        }

        return $this;
    }
962536
public function contextFactory(ContextFactory $instance = null, array $contexts = [])
    {
        if ($instance === null) {
            if ($this->_contextFactory === null) {
                $this->_contextFactory = ContextFactory::createWithDefaults($contexts);
            }

            return $this->_contextFactory;
        }
        $this->_contextFactory = $instance;

        return $this->_contextFactory;
    }
1221563
public function add($instance)
    {
        $class = get_class($instance);
        $entityPool = $this->pool[$class] ?? [];
        $entityPool[$instance->id] = $instance;
        $this->pool[$class] = $entityPool;

        return $instance;
    }
165158
public function instance($key, $instance)
    {
        if (!is_object($instance)) {
            throw new \InvalidArgumentException('Le paramètre 2 est invalide');
        }

        $this->instances[$key] = $instance;
    }
139930
public function addInstance(WidgetInstance $instance)
    {
        if (!$this->instances->contains($instance)) {
            $this->instances->add($instance);
            $instance->setContainer($this);
        }
    }
1062423
public function read($instance = null)
	{
		if (empty($instance))
		{
			$instance = $this->user_id;
		}

		if (empty($instance))
		{
			return false;
		}

		$this->clear();

		$db = \App::get('db');

		if (empty($db))
		{
			return false;
		}

		$result = true;

		if (is_numeric($instance))
		{
			if ($instance <= 0)
			{
				return false;
			}

			$query = "SELECT user_id,passhash,shadowLastChange,shadowMin," . "shadowMax,shadowWarning,shadowInactive,shadowExpire," . "shadowFlag FROM #__users_password WHERE user_id=" . $db->quote($instance) . ";";
		}
		else
		{
			$query = "SELECT user_id,passhash,shadowLastChange,shadowMin," . "shadowMax,shadowWarning,shadowInactive,shadowExpire," . "shadowFlag FROM #__users_password,#__users WHERE user_id=id" . " AND username=" . $db->quote($instance) . ";";
		}

		$db->setQuery($query);

		$result = $db->loadAssoc();

		if (!empty($result))
		{
			foreach ($result as $key => $value)
			{
				$this->__set($key, $value);
			}

			$this->_updatedkeys = array();
		}
		else
		{
			$hzp = Profile::getInstance($instance);

			if (is_object($hzp))
			{
				$this->__set('user_id', $hzp->get('uidNumber'));
				$this->__set('passhash', $hzp->get('userPassword'));
				$this->create();
			}
			else
			{
				return false;
			}
		}

		return true;
	}
577783
public function instance($abstract, $instance)
        {
            if (is_array($abstract)) {
                list($abstract, $alias) = $this->extractAlias($abstract);
                $this->alias($abstract, $alias);
            }
            $this->instances[$abstract] = $instance;
        }
1074738
public function exists($instance, string $method)
    {
        $reflection = new ReflectionClass(get_class($instance));

        return $reflection->hasMethod($method);
    }
204036
public static function getInstance($cloneInstance = false): Factory
	{	
		if ($cloneInstance === true || self::$cloneInstances === true)
		{
			return clone self::$sharedFactoryInstance;
		}

		return self::$sharedFactoryInstance;
	}
422927
public static function instance($instance = null)
	{
		if ($instance !== null)
		{
			if ( ! array_key_exists($instance, static::$_instances))
			{
				return false;
			}

			return static::$_instances[$instance];
		}

		if (static::$_instance === null)
		{
			static::$_instance = static::forge();
		}

		return static::$_instance;
	}
826948
public function lazyLoad($instance, $relation_name)
    {
        if (($relation = $this->get($instance, $relation_name)) === false) {
            throw new InvalidValueException($relation);
        }

        $type = $relation->type;
        $referenceModel = $relation->referenceModel;
        $valueField = $relation->valueField;
        if ($type === Relation::TYPE_HAS_ONE) {
            return $referenceModel::query()->whereEq($relation->keyField, $instance->$valueField)->setFetchType(false);
        } elseif ($type === Relation::TYPE_BELONGS_TO) {
            return $referenceModel::query()->whereEq($relation->keyField, $instance->$valueField)->setFetchType(false);
        } elseif ($type === Relation::TYPE_HAS_MANY) {
            return $referenceModel::query()->whereEq($relation->keyField, $instance->$valueField)->setFetchType(true);
        } elseif ($type === Relation::TYPE_HAS_MANY_TO_MANY) {
            $ids = $instance::values($relation->keyField, [$valueField => $instance->$valueField]);
            /** @var \ManaPHP\Model $referenceInstance */
            $referenceInstance = is_string($referenceModel) ? new $referenceModel : $referenceModel;
            return $referenceModel::query()->whereIn($referenceInstance->getPrimaryKey(), $ids)->setFetchType(true);
        } elseif ($type === Relation::TYPE_HAS_MANY_VIA) {
            $via = $relation->keyField;
            /** @var \ManaPHP\Model $reference */
            $reference = new $referenceModel();
            $ids = $via::values($reference->getPrimaryKey(), [$valueField => $instance->$valueField]);
            return $referenceModel::query()->whereIn($reference->getPrimaryKey(), $ids)->setFetchType(true);
        } else {
            throw  new NotSupportedException(['unknown relation type: :type', 'type' => $type]);
        }
    }