Menu

PHP static: Static Methods, Properties and self vs static

A static method or property belongs to the class, not to an object: you call it with ClassName::method() and read it with ClassName::$count, no new needed. Learn self:: vs static:: (late static binding), new static, static variables inside functions, and when static is the wrong tool.

This page includes runnable editors - edit, run, and see output instantly.

A static method or property belongs to the class itself rather than to any object. You call a static method with ClassName::method() and read a static property with ClassName::$property, without creating an object first. Inside the class, self:: refers to the same members.

There is no new Temperature() anywhere. Static members are useful for helpers that need no object state (conversions, factories, validation) and for data that belongs to the class as a whole, like a counter.

Static properties are shared by all objects

A normal property has one value per object. A static property has one value for the whole class, so every object sees the same one. Here each new User increments a shared counter and receives the next ID:

Note the $ in self::$nextId: static properties keep it after ::, while constants (self::MAX) do not. A static property lives for one PHP request only. On a typical website (PHP-FPM or Apache) it starts over with every page load, so it is not a place to store data between visitors.

Static methods have no $this

A static method runs without an object, so $this does not exist inside it. A static method can call other static methods with self::, but it cannot read normal properties. Calling a normal method statically fails as well:

The opposite direction is fine: a normal method can call static ones, as withSeparator() does with self::make().

self vs static: late static binding

self:: points to the class where the code is written. static:: points to the class the method was called on at runtime. The difference only shows up with inheritance, and it matters most for factory methods:

Post::createWithSelf() returns a Model, which is almost never what the caller wanted. static resolves to Post because that is the class named in the call; PHP calls this late static binding. Use static:: and new static() in methods meant to be inherited, and self:: when you really mean this exact class. The same rule applies to constants: static::TABLE picks up a child's own TABLE, while self::TABLE always reads the one in the class where the code is written.

Inherited static properties are shared

A child class shares its parent's static property unless it declares its own. This is the classic static surprise: two subclasses counting into one number.

Apples has no $count of its own, so Apples::$count is Counter::$count, and both print 2. Pears redeclares the property and counts on its own. A static property declared in a trait behaves differently: each class using the trait gets a separate copy.

Static variables inside functions

static has a second meaning inside a function body: a static variable keeps its value between calls. The initial value is assigned only the first time the function runs:

The second slowSquare(12) skips the computation. Like static properties, these values reset at the end of each request. The variables page covers variable scope in general.

When static is the wrong choice

Static methods are convenient, and that makes them easy to overuse. A class whose methods all read and write static properties is global state with a class name on it: every caller shares it, tests cannot get a fresh copy, and you cannot swap in a different implementation.

Good uses of static: pure helpers with no state (Slug::make()), named constructors (Color::fromHex()), and class-wide constants. If a method needs data that could differ between two uses, it wants an object.

Frequently Asked Questions

How do I call a static method in PHP?

With the class name and ::: MathHelper::square(4). Inside the same class use self::square(4) or static::square(4). No object is needed, and inside a static method there is no $this.

What is the difference between self and static in PHP?

self:: always means the class where the code is written. static:: means the class the method was actually called on, which can be a child class; this is called late static binding. So in a parent's create() method, new self() always builds the parent, while new static() builds whichever child called it.

What is a static property in PHP?

A property declared public static int $count = 0; belongs to the class, so there is one value shared by every object of that class (and its children, unless a child redeclares it). Read and write it with ClassName::$count or self::$count; note the $.

Can I call a static method on an object in PHP?

Yes. $obj->square(4) and $obj::square(4) both call the static method of the object's class, but there is still no $this inside it. Calling a non-static method as ClassName::method() is the direction that fails: since PHP 8 it throws an Error.

Can I use $this in a static method?

No. A static method runs without an object, so $this is not defined there and using it throws an Error ("Using $this when not in object context"). Use self:: or static:: to reach other static members.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED