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

How can I late static binding in inherited class with variable variables of PHP

The thing is, suppose we have three classes A, B and C. B and C inherit from A.
0
votes
1 answer

Late Static Bindings in perl

Are there any grammar for indicating "Late Static Bindings" in perl?? In php, there is. http://php.net/manual/en/language.oop5.late-static-bindings.php I'm just looking for them for perl.
cheek
  • 1
0
votes
1 answer

Unset child object static variable from abstract parent class php

I am using Laravel and it's Validators. I have the following code in my controller: class ResellerController extends BaseController{ public function add() { //some code before $userValidator = new…
0
votes
1 answer

Late Static Binding to unset non-static array in extended class

I apologize in advance for this being a bit convoluted. If it were less so, I might not be considering the question. I am working with a platform that allows for extending certain core classes but not others but due to the sequence of events I'm not…
Anthony
  • 36,459
  • 25
  • 97
  • 163
0
votes
2 answers

Accessing incorrect static property from within non static method in PHP

I am experiencing something in PHP that seems very odd. I am trying to access a static property from a non static method. I need to use the static keyword to access this property as it can have different values in each child class. However, instead…
Kenneth Spencer
  • 1,392
  • 12
  • 15
0
votes
1 answer

Set cache handler at object level?

Say I have a base object, CacheObject: abstract class CacheObject { protected static $handler = null; public static function setCacheHandler($handler) { static::$handler = $handler; } public static function…
Kaitlyn2004
  • 3,879
  • 4
  • 16
  • 16
0
votes
1 answer

is it good to use self:: or static:: on non static function?

i am doing some oop in php and when i just wanted to know if its good to do this? when i use $this->functionName(), it works fine and i even tried with self:: and static:: and they work as i expected: self:: will use the parent method implementation…
user2707590
  • 1,066
  • 1
  • 14
  • 28
0
votes
3 answers

Call static properties within another class in php

I have problem about calling a static property of a class inside another class. Class A { public $property; public function __construct( $prop ) { $this->property = $prop; } public function returnValue(){ return…
Pars
  • 4,932
  • 10
  • 50
  • 88
0
votes
1 answer

Caching via static properties in PHP

I have a number of classes that extend an abstract DatabaseRecord class. Essentially, the DatabaseRecord class handles some common functions that all of the child classes use in interacting with the database (e.g. searching by id, updating,…
waiwai933
  • 14,133
  • 21
  • 62
  • 86
0
votes
1 answer

PHP issue with inheritance and LSB

I'm having the following problem:
Jonathan H
  • 7,591
  • 5
  • 47
  • 80
0
votes
2 answers

Is this normal?

I don't get how this late static binding works. abstract class A{ final public static function doprint(){ print get_called_class() . '
'; } public static function wrapper(){ self::doprint(); static::doprint(); } } class…
thelolcat
  • 10,995
  • 21
  • 60
  • 102
0
votes
3 answers

Late static binding in PHP, vars are being shared between child classes

Maybe my question has been asked several times, but... I have the following code abstract class ParentClass { protected static $count=0; public static function inc() { static::$count++; } public static function…
Ruslan Polutsygan
  • 4,401
  • 2
  • 26
  • 37
0
votes
3 answers

PHP use late static binding to get calling function?

Is it possible to get information (filename, line, function ...) of the calling function by using late static binding?
NaN
  • 3,501
  • 8
  • 44
  • 77
0
votes
1 answer

PHP Late Static Binding error

I am trying to learn how to use LSB. I am trying to separate my common db methods into class DatabaseObject and extend it to all classes that will be using them. Common db methods being find_by_id(), create(), delete(), etc. $username =…
Jonathan Eckman
  • 2,071
  • 3
  • 23
  • 49
-2
votes
1 answer

How the constructors are getting called without the objects being created? Why the constructor is not getting called again in same manner?

I'm using PHP 7.1.11 Consider below working code and its output : '; } public static function…
PHPLover
  • 1
  • 51
  • 158
  • 311
1 2 3 4 5
6