aloyuu
V2EX  ›  问与答

PHP Class 两次继承,实例化两次, 祖先级__construct 执行几次?

  •  
  •   aloyuu · Jan 20, 2019 · 1880 views
    This topic created in 2679 days ago, the information mentioned may be changed or developed.

    calss db 使用 __construct 连接数据库,返回 pdo 对象变量

    class base extends db

    class list extends base

    $aaa = new base(); ........... .............

    $bbb = new list(); ........... .............

    实例化两个类. db 中的 __construct 会被执行几次?

    3 replies    2019-01-21 10:22:03 +08:00
    imdong
        1
    imdong  
       Jan 21, 2019   ❤️ 1


    看图说话
    zgxxx
        2
    zgxxx  
       Jan 21, 2019   ❤️ 1
    0 次,每次继承。上一个构造函数都会被覆写,建议百度下 parent::__construct
    stephanew
        3
    stephanew  
       Jan 21, 2019   ❤️ 1
    一楼都重写了,还不加 parent::_construct 当然不会执行了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3059 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 125ms · UTC 02:35 · PVG 10:35 · LAX 19:35 · JFK 22:35
    ♥ Do have faith in what you're doing.