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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:06:45+00:00 2026-05-14T07:06:45+00:00

I want to have a class property that allow for an expression to take

  • 0

I want to have a class property that allow for an expression to take place on the right side of the equals sign. All versions of PHP choke on the following code, but it is written in this way to allow for easier extendibility in the future.

/* Example SDK Class */
class SDK
{
    /* Runtime Option Flags */
    // Strings
    #  0: Makes no change to the strings.
    var $STRING_NONE        = (1 << 0);
    #  1: Removes color codes from the string.
    var $STRING_STRIP_COLOR = (1 << 1);
    #  2: Removes language codes from the string.
    var $STRING_STRIP_LANG  = (1 << 2);
    #  3: Removes all formatting from the string.
    var $STRING_STRIP       = SELF::STRING_STRIP_COLOR & SELF::STRING_STRIP_LANG;
    #  4: Converts color codes to HTML & UTF-8.
    var $STRING_HTML        = (1 << 3);
    #  8: Converts color codes to ECMA-48 escape color codes & UTF-8.
    var $STRING_CONSOLE     = (1 << 4);
    # 16: Changes player names only.
    var $STRING_NAMES       = (1 << 5);
    # 32: Changes host names only.
    var $STRING_HOSTS       = (1 << 6);
    function SDK($fString = SELF::STRING_HTML & SELF::STRING_NAMES & SELF_HOST)
    {
        // constructor code.
    }
}

$SDK &= new SDK(SDK::STRING_NONE);

(1 << 0) seems like very basic syntax to me, and is not fathomable why PHP would not allow for such a thing. Can anyone think of a work around that would maintain readability and future expandability of the following code?

  • 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-14T07:06:45+00:00Added an answer on May 14, 2026 at 7:06 am

    When declaring a class constant or property in PHP you can only specify a primitive values for default values. So for instance, this class declaration won’t work:

    class TEST {
     const ABC = 2 * 4;
     const DEF = some_function();
     static $GHI = array(
       'key'=> 5 * 3,
     );
    }
    

    But this class declaration will:

    class TEST {
     const ABC = 8;
     static $GHI = 15;
    }
    

    These rules apply to default values for class constants/properties – you can always initialize other variables with the results of an expression:

    $a= array(
     'a'=> 1 * 2,
     'b'=> 2 * 2,
     'c'=> 3 * 2,
    );
    

    The reason for this class declaration behavior is as follows: expressions are like verbs. They do something. Classes are like nouns: they declare something. A declarative statement should never produce the side-effects of an action statement. Requiring primitive default values enforces this rule.

    With this in mind we can refactor the original class as follows:

    class SDK
    {
    
        static protected $_types= null;
    
        static public function getType($type_name) {
            self::_init_types();
            if (array_key_exists($type_name, self::$_types)) {
                return self::$_types[$type_name];
            } else {
                throw new Exception("unknown type $type_name");
            }
        }
    
        static protected function _init_types() {
            if (!is_array(self::$_types)) {
                self::$_types= array(
                    'STRING_NONE'=> 1 << 0,
                    // ... rest of the "constants" here
                    'STRING_HOSTS'=> 1 << 6
                );
            }
        }
    
        function __construct($fString = null) {
            if (is_null($fString)) {
                $fString= self::getType('STRING_NONE') & self::getType('STRING_HOSTS');
            }
            var_dump($fString);
        }
    
    }
    
    $SDK &= new SDK(SDK::getType('STRING_HOSTS')); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class with a property which (the get method) I want
I have a class that I want to use to store properties for another
I have a class on which I want to allow several (~20+) configuration options.
I have a class that i want to push_back into a deque. The problem
I have a class with a ToString method that produces XML. I want to
I have a class that has some properties. And I want something that calculates
I have a class called forest and a property called fixedPositions that stores 100
I want to have a class which implements an interface, which specifies the specific
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
I have a class and I want to be able to iterate over a

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.