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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:27:20+00:00 2026-05-31T02:27:20+00:00

Can I do something like this? class Test { const FLAG1 = ‘flag1.’; const

  • 0

Can I do something like this?

class Test
{
    const FLAG1 = 'flag1.';
    const FLAG2 = 'flag2.';

    public function __construct() {}

    public function myMethod($flags)
    {
        echo $flags
    }
}

then use it like

$test = new Test();
$test->myMethod(Test::FLAG1|Test::FLAG2);

I tried to use this example but I get 2 as my echo.
I was just curious to see how I can use either CONST or DEFINE as parameters to a method.

I ask because I found this script that uses this technique and I found it curious

http://www.pgregg.com/projects/php/preg_find/preg_find.php.txt

  • 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-31T02:27:22+00:00Added an answer on May 31, 2026 at 2:27 am

    Yes, this is possible but not with the flag values you have now.

    Bitwise operators work on… well, the bits. If I do 1001 OR 0110, I get 1111. Therefore, if you want to be able to use constants like this, their values need to be powers of two.

    const FLAG1 = '1';
    const FLAG2 = '2';
    const FLAG3 = '4';
    

    You can then test for the method by AND-ing the parameter value of your function to a mask. For example, if someone passes in FLAG1|FLAG2, you can test for flag 2: 0011 AND 0010 = 0010 which is equal to FLAG2. In PHP, this would be ($flags & FLAG2) == FLAG2 to get a boolean of whether or not FLAG2 was passed in

    You can see all of the bitwise operators here: http://php.net/manual/en/language.operators.bitwise.php

    I should also point out that this would be generally considered poor form for PHP programming. Why do this when there are far better ways to pass in multiple parameters? Personally, I’d just accept an array.

    Does this make sense? If not, ask more detailed questions and I will follow up.

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

Sidebar

Related Questions

I want something like this public abstract class abc { public abstract void test();
I have a class which looks something like this: public class Test { private
What Swing class can I use to implement something like this? Add To List
In. NET, I can do something like this: public static T[] CreateAndFillArray<T>(int size) where
So, in a non-class type of situation, I can do something like this: int
I know you can do something like this: readlines(FileName) -> {ok, Device} = file:open(FileName,
How I can do something like this in C++: void my_print(format_string) { vector<string> data;
In Delphi you can do something like this : TArray = array[1..3] of byte;
A Tcl nested String can be something like this: {abc {xyz foo {hello world}}}
In SQL Server , I can do something like this: UPDATE tbl1 SET col2

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.