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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:07:33+00:00 2026-05-30T20:07:33+00:00

I am using Reflection against the following class: class Constant { const CONSTANT =

  • 0

I am using Reflection against the following class:

class Constant {
    const CONSTANT = 3;
    public $test1 = 'CONSTANT';
    public $test2 = CONSTANT;
}

When using ReflectionClass::getDefaultProperties(); I get the following notice:

PHP Notice: Use of undefined constant CONSTANT - assumed 'CONSTANT'

on this line of code:

$defaultValues = $reflectionClass->getDefaultProperties();

First, I wonder why I get the notice here (I mean, I can’t anticipate/avoid the notice even though the code is 100% correct)?

And second, when using var_export($defaultValues[3]), it outputs 'CONSTANT' which is normal because it has been casted to string.

So, how can I output CONSTANT instead of 'CONSTANT' for $test2 and still output a quote-delimited string for $test1?

Edit: I get CONSTANT for both cases ($test1 and $test2) but because of that I can’t differentiate between them. I want to be able to know: that is a string, or that is the name of a constant.

  • 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-30T20:07:34+00:00Added an answer on May 30, 2026 at 8:07 pm

    why I get the notice here?

    because you mean self::CONSTANT but tried to use global CONSTANT, e.g. your code assumes

    const CONSTANT = 3;              // global constant
    
    class Constant {
        const CONSTANT = 3;          // class constant
        public $test1 = 'CONSTANT';
        public $test2 = CONSTANT;    // refers to global constant
    }
    

    but you wanted to do this:

    class Constant {
        const CONSTANT = 3;
        public $test1 = 'CONSTANT';
        public $test2 = self::CONSTANT; // self indicated class scope
    }
    

    With the latter, this

    $reflectionClass = new ReflectionClass('Constant');
    var_dump( $reflectionClass->getDefaultProperties() );
    

    will give

    array(2) {
      ["test1"]=>
      string(8) "CONSTANT"
      ["test2"]=>
      int(3)
    }
    

    Is there a way to get ["test2"] => self::CONSTANT via Reflection? No. The Reflection API will evaluate the constant. If you want self::CONSTANT you’d have to try some of the 3rd party static reflection APIs.

    And obviously, if you want 'CONSTANT', write "'CONSTANT'".

    Regarding EDIT:

    I get CONSTANT for both cases ($test1 and $test2) but because of that I can’t differentiate between them. I want to be able to know: that is a string, or that is the name of a constant.

    $foo = CONSTANT means assign the constant value to the foo property. It does not mean assign the constant itself. By assigning the value to a property, it no longer is a constant value. It’s mutable. The “name of a constant” is represented as a string. You can use ReflectionClass::hasConstant to check whether that string happens to also be the name of a defined constant in the class or use defined for global constants.

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

Sidebar

Related Questions

Using reflection you can get pretty much everything relating to a class. You can
Using reflection, how can I get all types that implement an interface with C#
Does creating an object using reflection rather than calling the class constructor result in
I am using reflection class to invoke some methods which are on the some
What are the risks of using reflection? Does it go against OOP in any
I am using reflection to dynamically call some methods from extended class. Unfortunately one
I'm using reflection to get some member names as strings. I'm using a method
I'm generating a dynamic assembly using Reflection.Emit which includes a single class. I have
Using reflection I want to retrieve only the properties that have both a get
Using reflection, I need to investigate a user DLL and create an object of

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.