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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:15:25+00:00 2026-06-15T09:15:25+00:00

I have a class defined which has several constants defined through `const FIRST =

  • 0

I have a class defined which has several constants defined through `const FIRST = ‘something’;

I have instantiated the class as $class = new MyClass()

then I have another class that takes a MyClass instance as one of it’s constructors parameters and stores it as $this->model = $myClassInstance;

This works fine.

But I am wondering how I can access the constants from that instance?

I tried case $this->model::STATE_PROCESSING but my IDE tells me

Incorrect access to static class member.

and PHP tells me

unexpected ‘::’ (T_PAAMAYIM_NEKUDOTAYIM) in …

I know I can do MyClass::STATE_PROCESSING but I am wondering if there is a way to get them based off the instance?

  • 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-15T09:15:26+00:00Added an answer on June 15, 2026 at 9:15 am

    Seems like your on an older version of php? PHP 5.3 allows the access of constants in the manner you describe… However, this is how you can do it without that inherent ability:

    class ThisClass
    {
        const FIRST = 'hey';
    
        public function getFIRST()
        {
            return self::FIRST;
        }
    }
    
    class ThatClass
    {
        private $model;
    
        public function setModel(ThisClass $model)
        {
            $this->model = $model;
        }
    
        public function getModel()
        {
            return $this->model;
        }
        
        public function Hailwood()
        {
            $test = $this->model;
            return $test::FIRST;
        }
    }
    
    $ThisObject = new ThisClass();
    echo $ThisObject ->getFIRST(); //returns: hey
    echo $ThisObject ::FIRST; //returns: hey; PHP >= 5.3
    
    // Edit: Based on OP's comments
    $ThatObject= new ThatClass();
    $ThatObject->setModel($Class);
    echo $ThatObject->getModel()->getFIRST(); //returns: hey
    echo $ThatObject->Hailwood(); //returns: hey
    

    Basically, were creating a ‘getter’ function to access the constant. The same way you would to externally access private variables.

    See the OOP Class Constants Docs: http://php.net/manual/en/language.oop5.constants.php

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

Sidebar

Related Questions

I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have several final properties defined in a Java class with constructor which has
In my application I have a class which has properties of user-defined types like
I have a templated class (call it Foo ) which has several specializations. I
I have a class called MainGame, which is defined like this in my .h:
I have three tables which are defined as: class User(Base): __tablename__ = 'users' id
I have a class called Path for which there are defined about 10 methods,
i have a class User which extends Ebean Model. And i defined a dbfile
I have defined a ConfigurationProperty class, which is basicly a key-value pair (with key
I have a project which has just assimilated several apps from other projects 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.