In PHP, programs are saved and then directly run on the browser, the script is executed through a web server and we get the output. We don’t compile PHP programs manually but that does not mean it is never compiled. The PHP interpreter does that for you and runs it. So there are two phases, first, compile-time and second run time. During the compile time, the normal variables are replaced with their values but the static keywords are replaced only in the run time. Overriding a property in child class and creating the instance of the child class, so to get the overridden output, the late static binding concept is used by writing static keyword before using the property. Whenever a PHP interpreter gets the request to compile a function. If it sees any static property, then it leaves the property pending for run time and the property gets its value during runtime from the function it is being called. This is called late static binding.
This feature of late static binding was introduced in PHP 5.3 and above, previous versions will show a fatal error.
Below examples illustrate late static binding in PHP:
Example 1: In below code, we have a class Car and its child class newCar. We are getting some problem whenever we try to access the getOwner() function via scope resolution operator (without creating an object). Both classes have getCar() function but whenever we call getOwner() function through newCar class, it will inherit the getCar() function in class Car instead of newCar.
- Program: In this program we will not use the static binding we will use the old school self, then you will get the idea of late static binding.
<?php
// Car function
class
Car
{
public
static
$name
=
'Tesla'
;
public
static
function
getCar()
{
return
"The car name is : "
. self::
$name
;
}
public
static
function
getOwner()
{
echo
self::getCar();
}
}
class
newCar
extends
Car
{
public
static
function
getCar()
{
return
"The car name is : "
. self::
$name
.
" and owner is Anshu."
;
}
}
Car::getOwner();
echo
"\n"
;
newCar::getOwner();
?>
- Output:
The car name is : Tesla The car name is : Tesla
- Program 2: One thing we can do is that copy the getOwner() function from the Car class to newCar class but it can be done for small programs. What if your program contains 100 to 1000 functions. To solve this, the static keyword can be used instead of the self. The newCar class doesnot contains getOwner() function but still it inherits getCar() function of newCar. This happened because the getOwner() is calling getCar() in runtime instead of compile time. Runtime access of getOwner() function in Car class and not the compile or early access. In this way, we can get getOwner() function in newCar() class without creating an object.
<?php
// Car function
class
Car
{
public
static
$name
=
'Tesla'
;
public
static
function
getCar()
{
return
"The car name is : "
. self::
$name
;
}
public
static
function
getOwner()
{
echo
static
::getCar();
}
}
class
newCar
extends
Car
{
public
static
function
getCar()
{
return
"The car name is : "
. self::
$name
.
" and owner is Anshu."
;
}
}
Car::getOwner();
echo
"\n"
;
newCar::getOwner();
?>
- Output:
The car name is : Tesla The car name is : Tesla and owner is Anshu.
Example 2: Late static binding on const, it will work the same as static methods which means how it is being called. For const, it need not be necessarily static.
- Program:
<?php
class
One
{
const
MY_CONST = false;
public
function
selfConst()
{
return
self::MY_CONST;
}
public
function
staticConst()
{
return
static
::MY_CONST;
}
}
class
Two
extends
One
{
const
MY_CONST = true;
}
$two
=
new
Two();
// prints "no"
echo
$two
->selfConst() ?
'yes'
:
'no'
;
echo
"\n"
;
// prints "yes"
echo
$two
->staticConst() ?
'yes'
:
'no'
;
?>
- Output:
no yes
Where to use it ?
- Where a function is overridden and you want to display the new properties.
- Where the program is too big, and you cannot write the same function again and again.