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
2
votes
1 answer

Laravel late static binding as static::whereIn

Ok, I read and feel I have some understandings about PHP late static binding for methods and variables. But from line 28 in this code on Laravel 5, it uses with whereIn which is a Laravel Collection method. I don't understand what's going on here,…
shin
  • 31,901
  • 69
  • 184
  • 271
2
votes
2 answers

Can PHP do a fairly trivial inheritance thing?

I have the following code: abstract class AbstractParent { function __construct($param) { print_r($param); } public static function test() { return new self(1234); } } class SpecificClass extends AbstractParent {} When I invoke…
Yurii Rashkovskii
  • 1,122
  • 1
  • 10
  • 20
2
votes
2 answers

PHP late static bound referencing

Situation In this web app I am building there is a "bootstrap" sequence that defines (through constants) and initiates an extended controller. Currently, the controller keeps track of assets (script files, css, etc.) that will be deployed at the…
Simon
  • 816
  • 2
  • 7
  • 16
2
votes
1 answer

How to dynamically fetch class name of called class in parent:: call?

I'm trying to use get_called_class() on a parent class, and retrieve the parent class name rather than the subclass name. I can't use __CLASS__ in this case because I need it in a dynamic context since these methods are actually being inserted into…
Robert
  • 5,735
  • 3
  • 40
  • 53
2
votes
1 answer

Is there a middle-ground between self and static?

I am looking for a middle ground between SELF and STATIC, without the unexpected behavior of Late Static Binding. Below is my code, with practice results and expected results:
Mike
  • 1,968
  • 18
  • 35
2
votes
0 answers

PHP C-extension, making use of Late Static Binding

I'm trying to figure out how to make use of late static binding within a PHP C-extension, Example, I want to recreate this method: public final static function create() { return new static; } How is this possible using the Zend Engine? EDIT: I…
2
votes
3 answers

PHP Late Static Binding referencing calling class

I have a static function being called that is giving a strange error. Here is an example of the php code: class foo { public $stat; public function __construct() { $this->stat = stat::isValid('two'); } } class stat { …
Schleis
  • 41,516
  • 7
  • 68
  • 87
2
votes
3 answers

Request for clarification about OOP procedure in PHP

I am trying to write the following code in PHP class A { protected static $comment = "I am A" ; public static function getComment () { return self :: $comment; } } class B extends A { protected static $comment = "I am B" ; } echo…
user28490
  • 1,027
  • 8
  • 8
2
votes
2 answers

Prevent late static binding with static variable access from parent function

Given the following class hierarchy: class ParentClass { private static $_test; public function returnTest() { return static::$_test; } } class ChildClass extends ParentClass { // intentionally left blank } $child = new…
DudeOnRock
  • 3,685
  • 3
  • 27
  • 58
1
vote
2 answers

Practical examples of late static binding in PHP?

I understand how late static binding works, but I can't seem to come up with a time when I'd use it. The examples on the PHP site are nice, but don't show any kind of realistic usage. I'm just wondering if there are any examples of when LSB is…
Major Productions
  • 5,914
  • 13
  • 70
  • 149
1
vote
1 answer

Can I use Late Static Binding for non-static members?

The documentation says that: This feature was named "late static bindings" with an internal perspective in mind. "Late binding" comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be…
jwa
  • 122
  • 1
  • 7
1
vote
1 answer

Forwarding and non-forwarding calls - Late Static Binding

Note: Late static bindings' resolution will stop at a fully resolved static call with no fallback. On the other hand, static calls using keywords like parent:: or self:: will forward the calling information. Example #4 Forwarding and non-forwarding…
user19481364
1
vote
2 answers

Why does PHP execute method of "A" if static:: be resolved to "B"

In this example it first searches in B (because static:: resolves to B) and as it does not find it, it now searches in "A" and that is why it succeeds, right? or am I wrong? class A { private function foo() { echo "success!\n"; } …
1
vote
1 answer

syntax error, unexpected T_STATIC

I have this error while trying to use late static bindings. All I can find in google about this error is that people didn't have PHP5.3, but I have version 5.3.6. Could someone help me please ? Thanks class Media { private $nom, …
Julie
  • 11
  • 2
1
vote
2 answers

php late static binding revieve error expecting T_FUNCTION

I am new to OOP and I have been working on this example but I cannot seem to get rid of this error Parse error: syntax error, unexpected ';', expecting T_FUNCTION in C:\Program Files (x86)\Apache Software…
Brook Julias
  • 2,085
  • 9
  • 29
  • 44