drdawt9210 2012-07-08 21:21
浏览 20
已采纳

PHP是否为每个扩展同一父级的新类分配内存?

I am wondering if PHP will re-use the Parent class. With the following code:

// File parent.php
class Parent {
  $public foo = '';

  public function __construct($args) {
    // ..
  }

  // .. some functions
}

// File child1.php
class Child1 extends Parent {
  public function __construct() {

  }

  // .. Some overridden function
  // .. some extra functions
}

// File child2.php
class Child2 extends Parent {
  public function __construct() {

  }

  // .. some extra functions
}

Now to use either Child1 or Child2 you need to include the parent.php and childN.php. If I only use Child1 I can imagine they are somehow concatenated into a single being. However if I use Child2 on the same page.. would I get 2 'concatenated' beings, that gobble on memory or is PHP super smart and able to see this and only use/load/whatever the Parent only once.

This example is pretty simplified, but the Parent is pretty big and there are plenty of children!

  • 写回答

2条回答 默认 最新

  • doucheng9058 2012-07-08 21:27
    关注

    Short answer is "PHP super smart", the memory is only going to be used to store the class instances, the portion of memory to store the class definitions is negligible. You can also worry about number of inclusions, but in this particular case everything is far from slow.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了