Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6908527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:34:28+00:00 2026-05-27T08:34:28+00:00

Is there a way to enforce variable to be one of multiple types? Something

  • 0

Is there a way to enforce variable to be one of multiple types?

Something like:

class MyClass
{
    public function __construct(YourClass or TheirClass $incomingObject)
    {
        if($incomingObject instanceof YourClass){
            echo 'Im your class';
        }else{
            echo 'You\'re mine class';
        }
    }

    public function helloWorld(YourClass or TheirClass or RandomClass $greetable)
    {
        echo 'something..';
    }
}

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:34:28+00:00Added an answer on May 27, 2026 at 8:34 am

    You should define an interface for YourClass and then make all classes that will be passed to whatever support that interface. Then type hint to the interface instead of the class.

    interface IYourClass{
    
       public function doStuff();
    }
    
    class YourClass implements IYourClass {
    
       public function doStuff() {
          return "This is a '".__CLASS__."'";
       }
    }
    
    class MyClass {
       public function __construct(IYourClass $object){
          $this->message = $object->doStuff();
       }
    
       public function helloWorld() {
          echo "Hello world! ".$this->message;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick one: Is there any way to enforce types for variadic functions in PHP?
Is there a way to enforce/limit the types that are passed to primitives? (bool,
Is there a way to enforce a class only to be used in ASP.NET
Is there a way to enforce that a controller inherits from another one in
Is there a way to enforce constraint checking in MSSQL only when inserting new
Is there any way to enforce a template in Bugzilla to guide users fill
Is there any way in Java to enforce a minimum window size? The last
is there way how to get name ov event from Lambda expression like with
In PL/SQL Developer v7.1.x, is there way way to ignore large data types in
List<T> Foo<T>(Ilist list) where T : ?? is there any way to enforce T

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.