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
1
vote
3 answers

PHP late static bindings (new static): How to ensure subclass constructor compatibility and handle divergent constructors?

For some more complicated class hierarchy I was playin around with a minimal example for this problem a bit. This class is given - the method "createOrUpdate()" may be modified: class A { protected $a; protected $b; protected $c; …
Blackbam
  • 17,496
  • 26
  • 97
  • 150
1
vote
1 answer

what is the need of late static binding in php

I have a code in php, Select(); from…
mach2
  • 450
  • 2
  • 6
  • 24
1
vote
0 answers

Late Static Bindings issue

I have some troubles in my own project. I'm coding a simple exam system and for my convenience i using Late Static Bindings. I have next code:
1
vote
0 answers

php late static binding and 'self' calls

I've seen topic about this, and I understand how it works. But isn't it very confusing? Using self::someMethod() we intend to stop polymorphic behavior, and we expect to not depend anymore from possible overrides in child classes. But this example…
Dmitry J
  • 867
  • 7
  • 20
1
vote
1 answer

Why can't I directly use function return values as dynamic class names in PHP?

As of PHP 5.3, it is possible to use a variable as a class name, not only for object instantiation but even for static methods as well: $className = 'My\Name\Spaced\Thing'; $thing = $className::foo('hello world'); However, if I try to use the…
alexw
  • 8,468
  • 6
  • 54
  • 86
1
vote
2 answers

Objective-C Late Static Binding

I'm teaching myself Objective-C as a guilty pleasure, if you would. I have a self-proclaimed strong grasp of the Java language, so it's not a terribly difficult transition – it sure is fun though. But alas, my question! I'm attempting to reproduce…
Sean
  • 5,233
  • 5
  • 22
  • 26
1
vote
2 answers

Static binding and dynamic binding with no methods

I had an exam in my college on objected-oriented programming. One of the questions was about static binding and dynamic Binding. The question was as follows: Shape s; if(i==1) s = new Point(1,2); else s = new Rectange(10,20); //this is dynamic…
humazed
  • 74,687
  • 32
  • 99
  • 138
1
vote
1 answer

static::$property in trait returns data bound to the trait instead of class

I was wondering what the static keyword returns in a trait? It seems like it's being bound to the trait and not the class that uses it. For example: trait Example { public static $returned; public static function method() { if…
kfirba
  • 5,231
  • 14
  • 41
  • 70
1
vote
1 answer

Duplicating functions for late static binding

I am trying to get my head around late static binding, and through reading several stack overflow questions and the manual, I am there other than that I don't understand why, in all examples I have found (including the manual), the method that…
DJC
  • 1,175
  • 1
  • 15
  • 39
1
vote
1 answer

late static binding | without modifying parent class with `static` keyword

I have following parent and child class. class Parent_class { protected static function method_one() { echo "I am in Parent_class in method_one"; } protected function execute() { static::method_one(); } public…
Pranav
  • 2,054
  • 4
  • 27
  • 34
1
vote
1 answer

Undefined class constant 'self::STRING'

I've been struggling for a few days now with a completely weird bug: Here's the scenario (bear with me): I have one "framework" class, which I'll call F. I have some simple classes that extend F, one of them I'll call P. So what I have is: class F…
Guaycuru
  • 1,320
  • 1
  • 11
  • 12
1
vote
2 answers

Extending PHP classes

I have a separate classes for each MySQL table I have in my PHP project. The class would be something like this: class students { public static function find_all() { return self::sql_finder("SELECT * FROM ".get_class()); } } I use…
Naveen Margan
  • 58
  • 1
  • 9
1
vote
2 answers

How to find out what class was called previously?

I have a base class with many sub-classes, and a generic function to cache the results of a function. In the cache function, how do I figure out what sub-class was called? class Base { public static function getAll() { return…
Aistina
  • 12,435
  • 13
  • 69
  • 89
0
votes
1 answer

Late static bindings | static:: usage in a non-static context

Php manual for the late static bindings states, in the example of static usage in non-static context, that foo() will be copied to B? Is method inheritance copying with scope of the original function being maintained?
AlexBor
  • 151
  • 2
  • 12
0
votes
2 answers

How to make sure child method instantiates child instead of parent object?

I have a parent and a child class as below class Objet { ../.. static function findByNumeroDeSerie($table, $numeroDeSerie) { $db = new Db(); $query = $db->prepare("SELECT * from $table WHERE numeroDeSerie =…
Benoît
  • 360
  • 2
  • 14