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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:37:09+00:00 2026-05-17T02:37:09+00:00

I am trying to access static member of a class. my class is: class

  • 0

I am trying to access static member of a class.

my class is:

class A
{
    public static $strName = 'A is my name'
    public function xyz()
    {
        ..
    }
    ..
}
//Since I have bunch of classes stored in an array
$x = array('A');
echo $x::$strName;

I am getting error while printing. How can I print ‘A is my name’

  • 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-17T02:37:10+00:00Added an answer on May 17, 2026 at 2:37 am

    If A is a class, you can access it directly via A::$strName.

    class A {
        public static $strName = 'A is my name';
    }
    
    echo A::$strName; // outputs "A is my name"
    

    Update:

    Depending on what you have inside your array, whether its what I like to define as class objects or class literals could be a factor. I distinguish these two terms by,

    $objClasses = array(new A(), new B()); // class objects
    $myClasses = array('A','B');           // class literals
    

    If you go the class literals approach, then using a foreach loop with PHP5.2.8 I am given a syntax error when using the scope resolution operator.

    foreach ($myClasses as $class) {
         echo $class::$strName;
      //syntax error, unexpected '::', expecting ',' or ';'
    }
    

    So then I thought about using the class objects approach, but the only way I could actually output the static variable was with an instance of an object and using the self keyword like so,

    class A {
        public static $strName = 'A is my name';
    
        function getStatic() {
            return self::$strName;
        }
    }
    
    class B {
        public static $strName = 'B is my name';
    
        function getStatic() {
            return self::$strName;
        }
    }
    

    And then invoke that method when iterating,

    foreach($objClasses as $obj) {
        echo $obj->getStatic();
    }
    

    Which at that point why declare the variable static at all? It defeats the whole idea of accessing a variable without the need to instantiate an object.

    In short, once we have more information as to what you would like to do, we can then go on and provide better answers.

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

Sidebar

Related Questions

I have the following C++ classes. xyz.h class xyz { public: static int abc();
I'm trying to access a static method in a Utils class I created: public
How do I access a static member in a class with all static methods?
I am trying to access a form which is not static from another class
I am trying to access STATIC Variables from one java class to another but
Code I have the following class with a member interface: package com.example.withinterface; public class
I am trying to access a static variable within a class by using a
I'm trying to access a static property of a normal class (ClassA.StaticPropertyXX) in another
i created a singleton class and trying to access that class in other class
Code example from http://www.learncpp.com/cpp-tutorial/812-static-member-functions/ : class Something { private: static int s_nValue; }; int

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.