Will calling a static method directly using the class name in PHP trigger the constructor of the class or parent class?
淡淡烟草味
淡淡烟草味 2017-07-07 10:34:10
0
3
1422

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?

淡淡烟草味
淡淡烟草味

reply all(3)
学霸

You should test it yourself, the browser is the best teacher
Example 1:
<?php
class A{

function __construct(){echo"A::构造函数";}
static function myFun(){echo "你好,很高兴为你答题!";}

}
A::myFun();
?>
Example 2:
<?php
class 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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template