0

Here is an example that I am working on, and didn't know if it was considered bad coding?

class CounterClass
{
    private $counter = 0;
    static $counterOverall = 0;

   function CountUp()
   {
      $this->$counter++;
      self::$counterOverall++;
   }

}

2 Answers 2

1

No.

There is nothing inherently "bad" about accessing static variables from non-static methods. By itself, it's a perfectly acceptable practice.

Like most things in software engineering, it's just a tool, which can sometimes be perfect for a job at hand, while being a poor choice in other cases. It's up to the programmer to decide when best to use it.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't see any good reason to categorize this code under the "bad" category. Using static is giving you a shared memory across all instances of your class, which is just what you need in this case. I hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.