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

  • Home
  • SEARCH
  • 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 7887753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:39:47+00:00 2026-06-03T05:39:47+00:00

i wanna store some configuration data in some objects, and i’ve a problem… class

  • 0

i wanna store some configuration data in some objects, and i’ve a problem…

class B {
  const attr1 = 'something1';
  const attr2 = 'something2';
  const attr3 = 'something3';
}

class A {
  const attr1 = 'somethingA1';
  const attr2 = 'somethingA2';
  const attr3 = 'somethingA3';
  const b = <---- i wanna put here a reference of B, but B as a class (not an object...), i'm crazy?
}

I’m not sure if my example is clear… from A, i want to access to attr1 of B, like A::B::attr1, or something like this, there’s another way to do this? Or i’m wrong?

  • 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-03T05:39:48+00:00Added an answer on June 3, 2026 at 5:39 am

    There is no way to assign reference to a Class, nor is there a way to assign class constants at runtime. So your entire approach is pretty much impossible. What you can do is

    const b = 'B' 
    

    and as of PHP 5.3.0 you could then do

    $b = A::b;
    echo $b::attr1;
    

    but you cannot do A::b::attr1. This will raise a T_PAAMAYIM_NEKUDOTAYIM error. It’s a parser limitation. PHP cannot do this as of this writing.


    Because the class B contains a group of data of A, i want to store in b (of A) a complex data, i want to do like this because i wanna keep the code clean

    You can solve this easily by making B a composite of A, e.g. you either inject B into A when you create A

    class A
    {
        private $b;
    
        public function __construct(B $b)
        {
            $this->b = $b;
        }
    }
    $a = new A(new B);
    

    or create B inside A, e.g.

    class A
    {
        private $b;
    
        public function __construct()
        {
            $this->b = new B;
        }
    }
    

    Because B is just the data parts of A, you tunnel all public access through A instead of getting hold of B and then using B’s methods. So any code using A does not need to know there is some B inside A. This will allow you to change B easily without needing to worry about code that consumes A, e.g. to get attr1 you add a getter to A:

    public function getAttr1()
    {
        $b = $this->b;
        return $b::attr1;
    }
    

    You can mitigate the clumsy need for assignment when using properties instead of constants in B (constants are stupid in PHP anyway as you have to treat them as public API), e.g.

    class B
    { 
        private $attr1;
    
        public function getAttr1()
        {
            return $this->attr1;
        }
    }
    

    And then you can do in A:

    public function getAttr1()
    {
        return $this->b->getAttr1();
    }
    

    Even better would be not to expose the internals of B through A altogether though and only add public methods that do something with A. This will make your API much smaller and your code more OO.

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

Sidebar

Related Questions

I wanna to store some hidden data in my text box . Is there
I'm reading some GPS data (longitude,latitude) from a ServerManager class that stores it. The
I'm having a problem getting some data since I'm just starting to use LINQToEntities,
I wanna store small objects in unordered_map, just wondering if it may copy/release contained
I wanna place two child un-ordered lists side by side. They are having Class
My Windows application GUI is accepting some required application configuration fields from the user.
I have a array which store some country names im doing a search module
Hey guys, I have some questions would like to ask: I wanna ask how
any web service i can use to store small amounts of data (thinking XML
I have a tuple that i wanna store its elements, I'm trying to insert

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.