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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:11:49+00:00 2026-06-09T19:11:49+00:00

I hit a strange problem today and even as a PHP engineer i’m stumped

  • 0

I hit a strange problem today and even as a PHP engineer i’m stumped at this:

It seems you can access a class constant from an object instance such as:

class a {
    const abc = 1;
}
$a = new a();
var_dump($a->abc);

This will output null instead of the expected 1. I was able to do the following:

class a {
    const abc = 1;
}
$a = new a();
var_dump(a::abc);

But in the context of a sub object that doesn’t really know who the parent is exactly, i find it extremely annoying to do:

class a {
    const abc = 1;
}
$a = new a();
$c = get_class($a);
var_dump($c::abc);

Is it me or this is completly stupid, if not, please enlighten me why its working that way.

EDIT

Another way of doing it but it’s not really better:

class a {
    const abc = 1;
}
class b {
    public function getA(){
        return new a();
    }
}
$b = new b();
$c = $b->getA();
var_dump($c::abc);

This last example works more like what i am doing and experiencing…

  • 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-06-09T19:11:51+00:00Added an answer on June 9, 2026 at 7:11 pm

    I found a relatively nice and clean way to make my problem easier to live with. Here is the solution i’ve applied. It is not necessarely the best but for my uses it does exactly what i need.

    By creating a magic __get method, i intercept the request for the constant name from and instance point of view and i use a quick reflection to see if that constant exists and return it’s value.

    This allows me to actually use all in one line a pattern that looks like this:

    class a {
        const abc = 1;
        public function __get($key){
            $r = new ReflectionObject($this);
            if($r->hasConstant($key)){ return $r->getConstant($key); }
        }
    }
    class b {
        public function getA(){
            return new a();
        }
    }
    $b = new b();
    var_dump($b->getA()->abc);
    var_dump($b->getA()->def);
    

    Althought i’d have liked to do:

    var_dump($b->getA()::abc);
    var_dump($b->getA()::def);
    

    I guess this could be possible later in 5.4+ considering we finaly have array dereferencing, we could probably ask them to add static dereferencing soon.

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

Sidebar

Related Questions

today i hit a really annoing problem with wpf. i just want to align
I've hit this problem recently, and I've been stuck at it for a few
I have come across a strange problem with Zend_Framework, I can not load forms
Admittedly, this is a strange problem for me to have, but here is what
I'm seeing a strange behaviour, hopefully someone out there can explain this. I have
I encountered a strange problem today. Whenever i put a breakpoint in one of
Having a really strange problem. Scenario: PHP5.2.9, IIS7, PHP running as FastCGI. I have
I am experiencing a strange problem with asp.net forms authentication. This problem only occurs
I've recently hit a strange problem with my iOS app and have no idea
So I'm trying to debug this strange problem where a process ends without calling

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.