When you say Empty functions, more than lily your talking about __construct
and magic methods
The way they help us to determine authorization of an action, for example
public class MyClass
{
private function __construct(){}
}
This allows us to block the new MyClass
call, but can be overriden by calling a static method and doing new self()
There are also other reasons such as disallowed inheritance, like so:
#Class 1
public function Base
{
public function start()
{
echo "hello";
}
}
#Class 2
public function Layer extends Base
{
}
#Class 3
public function Layer extends Base
{
private function start(){}
}
looking at the above class 2 contains a callable method called start, but its the method from the parent, in class 3 the same occurs but its overridden by the local method as such, and as its private it disallows the call of the method.
getting a little deeper into OOP we have interfaces which tell a class it must contain a designated set of methods, so if a class implements such interface it will be required to define all the methods needed, otherwise it will throw an error, so programmers may create empty methods to keep the class structure.
In light of your new example, There is no specific reason for an empty public __constructor
apart from when its extending another class.
Here's an example:
class Main
{
public function __construct()
{
echo "Main";
}
}
class FirstSub extends Main
{
}
class SecondSub extends Main
{
public function __construct()
{
echo "SecondSub";
}
}
class ThirdSub extends Main
{
public function __construct()
{
echo "ThirdSub <br />";
parent::__construct();
}
}
As you can see from the structure we have one primary class called Main
and three sub class.
If we initialize the main class were going to get the constructor print Main
, but the results will vary with the sub classes as the constructor overrides the parent.
so initializeing the following will result in:
- Main:
Main
- FirstSub: `` - Nothing
- SecondSub:
SecondSub
- ThirdSub:
ThirdSub<br />Main
as you can so that using a blank construct has effect on parent classes.