search

Home  >  Q&A  >  body text

Will calling a static method directly using the class name in PHP trigger the constructor of the class or parent class?

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?

淡淡烟草味淡淡烟草味2726 days ago1448

reply all(3)I'll reply

  • 欧阳克

    欧阳克2017-07-07 10:36:10

    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();
    ?>

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-07-07 10:36:10

    1. No
    2. No

    The constructor is only called during instantiation. Static methods do not generate instances and will not call the constructor

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-07-07 10:36:10

    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.

    reply
    0
  • Cancelreply