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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:28:17+00:00 2026-06-12T07:28:17+00:00

Maybe my question has been asked several times, but… I have the following code

  • 0

Maybe my question has been asked several times, but…

I have the following code

abstract class ParentClass
{
    protected static $count=0;
    public static function inc()
    {
        static::$count++;
    }
    public static function getCount()
    {
        return static::$count;
    }
}

class FirstChild extends ParentClass
{

}
class SecondChild extends ParentClass
{

}

And I use it just like this

FirstChild::inc();
echo SecondChild::getCount();

It shows me “1”. And as you probably guess I need “0” 🙂

I see two ways:

  1. Adding protected static $count=0; to each derivative classes
  2. Make $count not integer but array. And do sort of such things in inc and getCount methods:

    static::$count[get_called_class()]++;

and

return static::$count[get_called_class()];

But I think these ways are a bit ugly. First – makes me copy/paste, what I’d like to avoid. Second – well, I don’t know:) I just don’t like it.

Is there a better way to achive what I want?

thanks in advance.

  • 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-12T07:28:18+00:00Added an answer on June 12, 2026 at 7:28 am

    No, you have exactly laid out the two most practical options to address this. PHP cannot work magic; when you declare a static protected property you get exactly that: one property. If the syntax you give did work that might be good news for everyone who needs to do that, but it would be absolutely horrible news for everyone else who expects PHP OOP to behave in a somewhat sane manner.

    And for the record, if you don’t need a separate counter for all derived classes without exception then I consider the explicit protected static $count = 0 in derived classes that do need one to be a beautiful solution: you want your own counter, you ask for one, and that fact remains written in the code for everyone to see.

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

Sidebar

Related Questions

Maybe this question has been asked many times before, but I never found a
This question has been asked a number of times, I have noted, but none
Maybe this has been asked before, but I couldn't find it. My question is
This question has been asked many times before but none of the previous ones
Maybe it has been asked somewhere, but I am trying to find my question
I know this question has been asked many times and we have gotten this
I know that this question has been asked too many times, but I think
This question seems to have been asked many times before, but I didn't see
I know this question has been asked few times over SO but none of
I know this question has been asked numerous times but I've tried everything. I

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.