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
5
votes
5 answers

Forget late static binding, I need late static __FILE__

I'm looking for the get_called_class() equivalent for __FILE__ ... Maybe something like get_included_file()? I have a set of classes which would like to know what directory they exist in. Something like this:
bobthecow
  • 5,047
  • 25
  • 27
4
votes
4 answers

How to indicate in php 7.1 that the return type is the current child type?

I have abstract class A{ public static function getSingle($where = []) { $classname = get_called_class(); $record = static::turnTheWhereIntoArecordFromDB($where); $model = new $classname($record); return $model; } } class…
Toskan
  • 13,911
  • 14
  • 95
  • 185
4
votes
1 answer

What is Call Forwarding and Static Calls in PHP or otherwise Late static binding?

One code sample I have got from a website, but it was difficult for me to understand the output. I am sharing the code : class A { public static function foo() { static::who(); } public static function who() { echo…
user3401141
  • 135
  • 4
  • 14
4
votes
3 answers

Is there a way to have PHP subclasses inherit properties (both static and instance)?

If I declare a base class as follows: abstract class Parent { protected static $message = "UNTOUCHED"; public static function yeah() { static::$message = "YEAH"; } public static function nope() { static::$message…
Alexander Trauzzi
  • 7,277
  • 13
  • 68
  • 112
4
votes
1 answer

late static binding in PHP

I am reading the php manual about the LSB feature, I understand how it works in the static context, but I don't quite understand it in the non-static context. The example in the manual is this:
Michael
  • 2,075
  • 7
  • 32
  • 46
3
votes
2 answers

Assigning static properties by reference behaves differently between PHP5 and PHP7

After upgrading from PHP 5 to PHP 7, the following code is producing different results: abstract class TheParent { public static $prop; public function __construct( $val ) { static::set_prop($val); } public static function…
joeymigs
  • 59
  • 7
3
votes
2 answers

Create two same static classes in PHP

I am trying to extend static class in PHP. What I am running into is that once I change the variable in one of the extend classes, all others classes are changes as well. This is what I am trying to do: class Fruit{ private static $name = Null; …
Blujacker
  • 68
  • 7
3
votes
1 answer

Which grandson called me?

Say I have this classes class Grandpa { public function call(){ // Well, I want to know who calls me here } } class Father extends Grandpa { } class GrandsonOne extends Father { } class GrandsonTwo extends Father { } Now, I call…
Michael
  • 4,786
  • 11
  • 45
  • 68
3
votes
1 answer

PHP late static binding doesn't work correctly

While coding and using late static binding in PHP I found some strange behaviour. A child object created with static() in its parent class can access the private methods of its parent. Here's an example: class Attachment { public static function…
oncode
  • 497
  • 7
  • 10
3
votes
1 answer

How to solve static:: call when its unavailable?

I have a php 5.2 on my server (cant update) and it drops error on a static::routin() call. How to solve it? Anyway, is there a way, to detect if this type of call is available, so that an intelligent call-mechanism can be added?
John Smith
  • 283
  • 5
  • 12
2
votes
1 answer

Assigning a class variable from subclass without constructor

lI am building a light-weight Model layer for my project's database access. I would like it to be in the spirit of Ruby on Rails. Instead of instantiating a new Model object, I want to use a singleton approach. Here is the current issue I am…
JohnnyStarr
  • 619
  • 8
  • 21
2
votes
2 answers

Parent static function calling static child variable

Here's a simplfied version of the classes I'm dealing with class A { static protected function getVal() { return self::$valB; } } class B extend A { static protected $valB = 'Hello'; } B::getVal(); // Hello Should…
Clutch
  • 7,404
  • 11
  • 45
  • 56
2
votes
1 answer

How to make 'self' in parent class refer to extended class in PHP?

I'm working with Wordpress and need to instantiate a new class similar to this oversimplified example (for an add_action() hook which receives some arguments): class A { public function __construct() { } public static…
ridgek
  • 65
  • 6
2
votes
1 answer

Interface constants, Late Static Bindings

interface PasswordBrokerContract { const PASSWORD_RESET = 'passwords.reset'; public function reset(array $credentials, Closure $callback); } class PasswordBroker implements PasswordBrokerContract { public function reset(array…
AlexBor
  • 151
  • 2
  • 12
2
votes
4 answers

Why do some languages prefer static method binding rather than dynamic?

Why is the default decision in C++, C#, and Ada 95 to use static method binding, rather than dynamic method binding.? Is the gain in implementation speed worth the loss in abstraction and re-usability?
Glove
  • 960
  • 6
  • 17
  • 30