As the title states, PHP calls static methods directly using class names. There are two problems:
1 Will the constructor of the current class be called?
2 Will the constructor of the parent class of the current class be called?
You should test it yourself, the browser is the best teacherExample 1:<?phpclass A{
function __construct(){echo"A::构造函数";} static function myFun(){echo "你好,很高兴为你答题!";}
}A::myFun();?>Example 2:<?phpclass A{
function __construct(){echo"A::构造函数";}
}class B extends A{
static function myFun(){echo "你好,很高兴为你答题!";}
}B::myFun();?>
1. No 2. No
The constructor is only called during instantiation. Static methods do not generate instances and will not call the constructor
No, the constructor method will only be called when a class is instantiated. Static methods are stored in the static code area and are loaded as the class is loaded.
You should test it yourself, the browser is the best teacher
Example 1:
<?php
class A{
}
A::myFun();
?>
Example 2:
<?php
class A{
}
class B extends A{
}
B::myFun();
?>
1. No
2. No
The constructor is only called during instantiation. Static methods do not generate instances and will not call the constructor
No, the constructor method will only be called when a class is instantiated. Static methods are stored in the static code area and are loaded as the class is loaded.