Search code examples
phpmysqlmongodblaravellaravel-5.3

Setting multiple instance types for an argument in a function


I've done some googling on this and can't seem to find much assistance. However, I was wondering if there is a way to check multiple Instance types for an argument in a function, so to check whether it is an instance of the User model or another model for example and if it is one of the two then allow it through. If not, throw an error. I am using PHP and Laravel.

The use case here is that I am migrating a database from SQL to Mongo, so for an interim period I'll have two user models (User and MongoUser) and I want to check against each of these to determine whether the argument is a User instance of some form.

public function setTo(User $user)
    {
        $this->user()->associate($user);

        return $this;
    }

Last resort, I can duplicate the functions, but this seems unnecessary duplication. And I'd rather not remove the Instance check altogether.

I wanted to know if there was a way to achieve this? If not, I'd be interested to hear people's thoughts on the best way to handle this generally.

Thanks!


Solution

  • Here's what you could do:
    Let both classes (User and MongoUser) extend a base class (UserBase??) and check for that.

    <?php
    
    class base {
    }
    
    class A extends base {
    }
    
    class B extends base {
    }
    
    class C {
    }
    
    function test(base $x) {
        var_dump($x);
    }
    
    
    test(new base()); // good
    test(new A());  // good
    test(new B());  // good
    test(new C());  // throws error because class C doesn't extend base class
    

    Another possibility would be to leave out the restriction in the method-declaration and do a manual test:

    public function setTo($user)
    {
        if($user instanceof User || $user instanceof MongoUser) {
            echo "is an instance of User or MongoUser";
            $this->user()->associate($user);
    
            return $this;
        } else {
           // throw an erorr
        }
    }