V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
aloyuu
V2EX  ›  问与答

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

  •  
  •   aloyuu · 2019-01-20 21:58:59 +08:00 · 1266 次点击
    这是一个创建于 1915 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    class base extends db

    class list extends base

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

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

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

    3 条回复    2019-01-21 10:22:03 +08:00
    imdong
        1
    imdong  
       2019-01-21 09:36:56 +08:00   ❤️ 1


    看图说话
    zgxxx
        2
    zgxxx  
       2019-01-21 10:15:23 +08:00   ❤️ 1
    0 次,每次继承。上一个构造函数都会被覆写,建议百度下 parent::__construct
    stephanew
        3
    stephanew  
       2019-01-21 10:22:03 +08:00   ❤️ 1
    一楼都重写了,还不加 parent::_construct 当然不会执行了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2797 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 642ms · UTC 11:37 · PVG 19:37 · LAX 04:37 · JFK 07:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.