The difference between "public" and "public static"?

The difference between "public" and "public static"?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The difference between "public" and "public static"?

In object-oriented programming, modifiers like public and private govern visibility, but their interaction with the static keyword often confuses even seasoned developers. You likely know that public allows access from outside the class definition, whereas private restricts access to the internal methods of that same class. However, adding static changes the fundamental contract of what is being accessed, shifting the focus from an instance's state to the class itself.

When you declare a member as public, you are granting external access to a specific instance variable or method. If you instantiate a class and call a public method, you are invoking behavior on that specific object. The memory footprint for these members exists only once per instance. This means every time you create an object, the memory allocated for its public fields is duplicated.

Consider a simple PHP example where we track a counter:

class Counter {
    public $count = 0;

    public function increment() {
        $this->count++;
    }
}

Here, $count belongs to the object. If you create two instances of Counter, each maintains its own independent counter value. Modifying one does not affect the other. This is the expected behavior for stateful data.

The confusion arises when you introduce static. A static member belongs to the class definition rather than any specific instance. It is allocated once in memory, regardless of how many times you instantiate the class. When you combine this with public, as in public static, you are exposing a shared resource that persists across all instances.

class Config {
    public static $appName = 'LaravelCompany'; // Shared across all instances
}

In this scenario, accessing $Config::$appName retrieves the value stored in the class scope. Changing it through one instance reflects immediately in another because they are reading and writing to the same memory address.

Why public static is dangerous for state

A common pitfall occurs when developers try to use public static variables to maintain application-wide state, such as request counts or session data, without realizing the implications. If you define a public static $counter in a model and increment it within a controller loop, you might expect it to reset per request or per user. Instead, if the static property is not explicitly cleared (e.g., via static::reset()), it retains its value between requests or across different parts of your application logic.

This leads to subtle bugs where counters inflate unexpectedly, or configuration values overwrite each other in ways that are hard to trace. The distinction is clear: use public for instance data that varies per object, and use public static only for constants or truly global settings that should remain identical for every single instance of the class.

Trade-offs in design

Choosing between these modifiers is a design decision regarding data isolation. public enforces isolation; each object manages its own life. public static enforces sharing, which simplifies access but couples instances together tightly. In frameworks like Laravel, you frequently encounter public static properties in service providers or facades because the framework treats these as singletons managing global application state.

If you need to share data between instances without creating a singleton pattern, consider passing data explicitly or using dependency injection instead of relying on a shared static variable. This keeps your code testable and prevents hidden dependencies that make refactoring difficult.

Understanding this distinction ensures you don't accidentally mutate global state when you intended to isolate object behavior. It also helps in debugging performance issues caused by unintended memory accumulation if static properties hold large arrays or objects unintentionally.

Our network