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 7582899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:32:59+00:00 2026-05-30T18:32:59+00:00

I have a class: class FetchMode { const FetchAll = 0; const FetchOne =

  • 0

I have a class:

class FetchMode 
{
 const FetchAll = 0;
 const FetchOne = 1;
 const FetchRow = 2;}

and a function:

function getRecordSet(FetchMode $FetchMode){ some switch cases }

I would like to use $FetchMode as switch case criteria but receiving an error:
Catchable fatal error: Argument passed to getRecordSet() must be an instance of FetchMode, integer given

this is how I call a function:

getRecordSet(FetchMode::FetchOne);

I would like to offer a list of possible choices in calling a function.
Is it possible in php?

  • 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-30T18:33:00+00:00Added an answer on May 30, 2026 at 6:33 pm

    You’ve hinted PHP to expect an instance of FetchMode (just like it says in the error message), but FetchMode::FETCH* passes the constant value. You’d have to use some sort of Enum instance (which we dont have natively in PHP. (Oh well, there is SplEnum but who uses that?)) or change the method signature to exclude the typehint.

    However, instead of a Switch/Case you could solve this more easily via Polymorphism and a Strategy pattern, e.g. instead of doing something like

    public function getRecordSet($mode)
    {
        switch ($mode) {
            case FetchMode::ALL:
                // code to do a fetchAll
                break;
            case FetchMode::ONE:
                // code to do a fetchOne
                break;
            default:
        }
    }
    

    which will increase the Cylcomatic Complexity of your class and forces changes to that class and FetchMode whenever you need to add additional FetchModes, you can do:

    public function getRecordSet(FetchMode $fetchModeStrategy)
    {
        return $fetchModeStrategy->fetch();
    }
    

    and then have an interface to protect the variation

    interface FetchMode
    {
        public function fetch();
    }
    

    and add concrete FetchMode classes for each supported FetchMode

    class FetchOne implements FetchMode
    {
        public function fetch()
        {
            // code to fetchOne
        }
    }
    class FetchAll …
    class FetchRow …
    

    This way, you’ll never have to touch the class with that getRecordSet method again because it will work for any class implementing that FetchMode inteface. So whenever you have new FetchModes, you simply add a new class, which is much more maintainable in the long run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class with a member function that takes a default argument. struct Class
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have class in server side and I want use method of this class
I have class MasterData whose instances contain some general information about all possible gadgets.
I have @manytomany relation in hibernate Like : Table Employee public class Employee implements
In my Grails domain I have something like the following class A { String
I have class like this: public class object { [Key] int number; String mystring;
I have class A that has some primitive attributes and also member of type
I have class like below template<class T> class Student { public: static Student& Instance();
I have class method that returns a list of employees that I can iterate

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.