douzhajie7168 2015-08-22 10:33
浏览 321
已采纳

实例化多个对象会占用大量内存吗?

I'm using OOP aproach in php.I'm just learning though. Each time I need to access a method in a class I instantiate a object.So I have quite a number of objects created in my project to do each task.Is there a way where I can only create one object and share throughout the project to do multiple method for different task?

Also in my class, I declared the variable first and then use them like $this->property = $assign_variable.Will declaring variable earlier would consume memory much?

I'm just concerned to approach the right and effective way of instantiating object and declaring class in OOP. Can anyone suggest please?

  • 写回答

1条回答 默认 最新

  • dongta5747 2015-08-22 13:48
    关注

    Having multiple instances of an object consumes more memory (much is relative), as every attribute of an object needs to have allocated memory. If you have an object that consumes, lets say, x bytes of memory for its attributes, then you will need n*x bytes of memory if you instantiate n objects in total (There is also a neglactable amount of memory used which needs to be used for the code, but the amount is constant). In normal use that shouldn't be a problem though (i.e. if you don't have an unusual huge amount of objects).

    If you need only one instance of a class through the whole program, I'd suggest you to use the Singleton design pattern 1].

    Here is an example:

    class Singleton {
        // This is where the one and only instance is stored
        private static $instance = null;
    
        private __construct() {
            // Private constructor so the class can't be initialized
        }
    
        // That's how you get the one and only instance
        public static function getInstance() {
            if (Singleton::$instance === null) {
                Singleton::$instance = new Singleton();
            }
    
            return Singleton::$instance;
        }
    
        public function doSomething() {
            // here you can do whatever you need to do with the one and only object of this class
        }
    }
    

    You can then use it very conveniently like this:

    Singleton::getInstance()->doSomething();
    

    So you are basically just storing the address of the object in one location, namely Singleton::$instance.

    Another option for you could be to use static methods. It is defined like above in the Singleton pattern:

    class StaticExample {
        // ...
    
        public static function doSomething() {
            // your code
        }
    }
    

    And can be accessed with StaticExample::doSomething();

    I also want to note that usually, you don't have much classes in a project which implements the Singleton design pattern or use the static keyword very often. If you want to use a lot of singletons or see yourself needing a lot of statics, you probably got something wrong and should post an example of your code on another site like Programmers Stack Exchange.

    1] Singleton design pattern on Wikipedia

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!