1

assume i have a class like so:

class ClassA{

    var $var = 'a1';
    static $static_var = 'a2';

    function get(){
        return $this->var;
    }

    static function get_static(){
        return self::$static_var;
    }

}

What happens in memory when I call this:

$class = new ClassA();
echo $class->get();

And also what happens when I call this:

echo ClassA::get_static();

When I create an instance of ClassA, what happens to the static members of the class? Does it take up more memory? Does it ignore the static members?

I want to keep a bunch of static functions within ClassA but I also want to create instances of ClassA. Should I separate static members from dynamic members?

1 Answer 1

1

I want to keep a bunch of static functions within ClassA but I also want to create instances of ClassA. Should I separate static members from dynamic members?

Answer: Static class members consuming memory once per per class. Regular class members consuming memory once per instance

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

2 Comments

so when i create 1000 instances of ClassA i have 1000 copies of variable $var and just 1 copy of $static_var?
i just wanted to know if the static members influenced how much memory an instance of a class takes up. i guess this answers my question

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.