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

Object static properties in late static binding

Problem solved, its a bug, see https://github.com/docker-library/php/issues/133 What is really weird about this is that Im using this all the time in my framework, but suddenly it stops working for this particular example. Before executing this…
Honza
  • 323
  • 3
  • 14
0
votes
1 answer

Can php call a static alias?

In php, is it possible for a method in a parent class to use an alias in a child class from within an instance of the child class? Parent class: class ParentClass { public function getNewFoo() { return new Foo(); } } Child…
Leo Galleguillos
  • 2,429
  • 3
  • 25
  • 43
0
votes
0 answers

PHP Late Binding Static with private __construct

I have an abstract class that has a function which uses PHP's Late Static Binding, as follows: abstract class MetaComponent { public static function do(...$args) { return new static(...$args); } } Then, I implemented the abstract…
Mateus Felipe
  • 1,071
  • 2
  • 19
  • 43
0
votes
1 answer

Inheritance of a private method and Late Static Binding in php

So I've been reading official PHP documentation on Late Static Bindings and came across a confusing example: foo(); …
0
votes
2 answers

Use static parent instance with extended instance values

I have a main class abstract class Database { protected $table; public function where(array $params) { // ... } public function get() { // ... } } and then I use extended versions of the class: Users…
Peon
  • 7,902
  • 7
  • 59
  • 100
0
votes
1 answer

Override some function cakephp (Late Static Bindings )

I use CakePHP 2.9.8 to develop an old application that was written 7 years ago and developed until now. unfortunately the first developer add some codes in CakePHP's library and for migrating to the 3th version of CakePHP I need to transfer the…
Malus Jan
  • 1,860
  • 2
  • 22
  • 26
0
votes
1 answer

Why in late static binding child class gets data from parent and current methods

Ok, the title is hard to understand, but I was trying to understand about late static binding, and I saw this answer https://stackoverflow.com/a/35577069/1640606 Which shows the difference as being between those two example: Note, self::$c class…
ʎɹnɔɹǝW
  • 811
  • 2
  • 8
  • 14
0
votes
2 answers

Difference between these two OOP scenarios?

I am confused what the difference or the performance gain would be between these 2 scenario's. Why would one be chosen over the other? Parent class: class exampleB { public function __construct($arg1, $arg2) { // Do something with…
Stephan-v
  • 19,255
  • 31
  • 115
  • 201
0
votes
0 answers

Late static binding works as not expected

Here is the code of registry I'd like to use. But static doesn't work as it should. In this example it always returns 2 (while 1 is expected). What can it be?
Toletov
  • 55
  • 7
0
votes
1 answer

defining called class properties via parent class

I have written a parent class (using late static binding) from which my database classes are inherited. I'm trying to write a constructor to assign each table column as a public property of the child classes. So far i could write the constructor in…
MehdiK
  • 37
  • 6
0
votes
3 answers

Abstract Factories not possible in php < 5.3?

I was working on an abstract class to save on some code for a couple of classes. These classes are all factories that instantiate themselves through different static calls. I could save some code by putting all those methods in an abstract class.…
user151841
  • 17,377
  • 29
  • 109
  • 171
0
votes
0 answers

PHP - Calling a static method from the parent class

I have the following class hierarchy: MDLUser inherits from MDLPersistentObject In MDLPersistentObject I have a static method which returns an array. In MDLUser I override the static method so that it returns the MDLPersistentObject’s array, adds…
Adam Carter
  • 4,741
  • 5
  • 42
  • 103
0
votes
1 answer

get_called_class hack not working with eval-code

I am using a ge_called_class hack for allowing late static binding in php version 5.2 (found here). I have the following in my code: # db_record.php $ac = "ForumThread"; $objects = $ac::find("all"); This will not work in php 5.2 for some reason,…
Ekampp
  • 755
  • 1
  • 6
  • 9
0
votes
1 answer

using late staic binding variable with another class

I have created a class for writing sql query in which i have used late static binding concept and i am trying to call its insert method in different class to insert the values here is sqlQuery class class sqlQuery { public static $table="…
imrahul
  • 41
  • 6
0
votes
1 answer

appending static variable within sql query

I am trying to use the late static binding concept during insertion but I am getting a syntax error when I am writing this statement: I am using php version 5.3.8 $resultArray = $this->connection->query("insert into " static::$table "(title,link)…
imrahul
  • 41
  • 6