Questions tagged [late-static-binding]

In PHP, a Late Static Binding is a way for a static parent class method or variable to refer to the current class that is being executed via the static:: scope resolutor

Late Static Bindings were created in PHP 5.3 to overcome the problem inherent with the self:: scope resolutor. Take the following code

Class Horse {
    public static function whatToSay() {
         echo 'Neigh!';
    }

    public static function speak() {
         self::whatToSay();
    }
}

Class MrEd extends Horse {
    public static function whatToSay() {
         echo 'Hello Wilbur!';
    }
}

You would expect that the MrEd class will override the parent whatToSay() function. But when we run this we get something unexpected

Horse::speak(); // Neigh!
MrEd::speak(); // Neigh!

The problem is that self::whatToSay(); can only refer to the Horse class, meaning it doesn't obey MrEd. If we switch to the newer static:: scope resolutor, we don't have this problem. This newer method tells the class to obey the instance calling it. This we get the inheritance we're expecting

Class Horse {
    public static function whatToSay() {
         echo 'Neigh!';
    }

    public static function speak() {
         static::whatToSay(); // Late Static Binding
    }
}

Horse::speak(); // Neigh!
MrEd::speak(); // Hello Wilbur!

Resources

90 questions
585
votes
3 answers

New self vs. new static

I am converting a PHP 5.3 library to work on PHP 5.2. The main thing standing in my way is the use of late static binding like return new static($options); , if I convert this to return new self($options) will I get the same results? What is the…
Mike
  • 12,359
  • 17
  • 65
  • 86
149
votes
8 answers

What exactly are late static bindings in PHP?

What exactly are late static bindings in PHP?
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
53
votes
2 answers

Why return new static? (PHP)

Why some developers create one method that returns new static? What is the reason to have a method that returns new static? I am not asking what is the difference between static and self, or what static & self mean. For example, here is one simple…
PeraMika
  • 3,539
  • 9
  • 38
  • 63
20
votes
3 answers

PHPDoc and late (static or dynamic) binding

Most PHP IDEs rely on phpdoc to get hints about the type of an expression. Yet, I use frequently this pattern, which doesn't seem to be covered: class Control { private $label = ''; /** @return ??? */ public static function Make(){…
linepogl
  • 9,147
  • 4
  • 34
  • 45
15
votes
4 answers

Return type "self" in abstract PHP class

Trying to make an abstract class to partially implement functionality of its' child classes and force a contract upon them required for this implementation, I use the following construct: abstract class Parent { public static function…
Simon
  • 1,172
  • 12
  • 21
15
votes
3 answers

Inherit static properties in subclass without redeclaration?

I'm having the same problem as this guy with the application I'm writing right now. The problem is that static properties are not being inherited in subclasses, and so if I use the static:: keyword in my main class, it sets the variable in my main…
David
  • 337
  • 3
  • 5
14
votes
3 answers

Is it possible to overuse late static binding in PHP?

Starting with version 5.3, PHP supports late binding for static methods. While it's an undoubtedly useful feature, there are only several cases where its use is really necessary (e.g. the Active Record pattern). Consider these examples: 1.…
Ignas R
  • 3,314
  • 2
  • 23
  • 27
13
votes
3 answers

How do I call a static child function from parent static function?

How do I call child function from parent static function ? In php5.3 there is a built in method called get_called_class() to call child method from parent class. But my server is running with php 5.1. Is there any way can do this ? I want to call it…
Sahal
  • 4,046
  • 15
  • 42
  • 68
10
votes
1 answer

PHP 5.3: Late static binding doesn't work for properties when defined in parent class while missing in child class

Take a look at this example, and notice the outputs indicated.
OCDev
  • 2,280
  • 3
  • 26
  • 37
8
votes
1 answer

static:: vs. self:: - are there any downsides?

In this StackOverflow question I learned that self:: was not inheritance-aware where static:: was (in PHP). When it comes to defining a bunch of constants within a class, if you want to override those constants in a subclass to change default…
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
7
votes
1 answer

How to get child class name from parent class

I'm trying to accomplish this without requiring a function on the child class... is this possible? I have a feeling it's not, but I really want to be sure...
Ben
  • 60,438
  • 111
  • 314
  • 488
7
votes
1 answer

Why doesn't late static binding work with variables in PHP 5.3?

Let's start off with some code: class Super { protected static $color; public static function setColor($color){ self::$color = $color; } public static function getColor() { return self::$color; } } class…
Johan Fredrik Varen
  • 3,796
  • 7
  • 32
  • 42
7
votes
1 answer

Should I use new self or new static?

I work on a proprietary project that uses quite a lot of factories of one form or another. Most of them don't instantiate the class by name, fortunately, but whether new self() or new static() is used to instantiate varies depending on the…
Mikkel
  • 1,192
  • 9
  • 22
6
votes
3 answers

See if a static property exists in a child class from the parent class (late static binding)?

Code in parent class: foreach(static::$_aReadOnlyDatabaseTables AS $TableName => $aColumns){ // Do something } This works when $_aReadOnlyDatabaseTables is defined in the child class, but throws an error when $_aReadOnlyDatabaseTables is absent.…
Nick
  • 10,904
  • 10
  • 49
  • 78
5
votes
1 answer

PHP 5.2 Equivalent to Late Static Binding (new static)?

I am trying to make a script that is built for php 5.3 work on a php 5.2 server. The script uses a lot of late static binding like: return new static($options); What is the equivalent to this in php 5.2? would it be new self somehow? Or is it not…
Mike
  • 12,359
  • 17
  • 65
  • 86
1
2 3 4 5 6