dongpu3898 2016-10-16 09:16 采纳率: 100%
浏览 18
已采纳

在最后一堂课中只破坏一次。 PHP

I want in PHP to use destruct function only once. I want it at the end of the last class that extended the parent class. Like this:

class Thing
{
    public function __destruct()
    {
        echo "Destructed";
    }
}

class A extends Thing
{
    public function __construct()
    {
        echo "Class A initialized";
    }

    /* Here I DON'T WANT the destructor of the Thing class */
}

class B extends Thing
{
    public function __construct()
    {
        echo "Class B initialized";
    }

    /* Here I WANT the destructor of the Thing class */
}
  • 写回答

1条回答 默认 最新

  • dongshadu2546 2016-10-16 09:20
    关注

    Implement your own destructor in A class. Then it will be called instead of parent destructor. Leave B class without changes:

    class Thing
    {
        public function __destruct()
        {
            echo "Destructed";
        }
    }
    class A extends Thing
    {
        public function __construct()
        {
            echo "Class A initialized";
        }
    
        public function __destruct()
        {
            // Do nothing
        }
    }
    class B extends Thing
    {
        public function __construct()
        {
            echo "Class B initialized";
        }
        /* Here I WANT the destructor of the Thing class */
    }
    

    Test run with:

    $a = new A;
    $b = new B;
    

    outputs:

    Class A initialized
    Class B initialized
    Destructed
    

    Another option is checking class name in __destruct in parent class:

    class Thing
    {
        public function __destruct()
        {
            if ('A' == get_called_class()) {
                echo "DestructedA" . PHP_EOL;
            }
    
        }
    }
    

    In this case you don't have to write destructors in child classes.

    Update:

    Simple example for storage of objects:

    class ThingsStorage
    {
        // here we just store number of created objects
        protected static $storageSize = 0;
    
        // Add 1 when new object created
        public static function increment()
        {
            self::$storageSize++;
        }
    
        // Substract 1 when object destructed
        public static function decrement()
        {
            self::$storageSize--;
        }
    
        // get current size
        public static function getSize()
        {
            return self::$storageSize;
        }
    
    }
    
    class Thing
    {
        public function __destruct()
        {
            // object destroyed - decrement storage size
            ThingsStorage::decrement();
            // if no objects left - call special code
            if (ThingsStorage::getSize() == 0) {
                echo "Destructed" . PHP_EOL;
            }
        }
    }
    
    class A extends Thing
    {
        public function __construct()
        {
            echo "Class A initialized" . PHP_EOL;
            // increment storage size
            ThingsStorage::increment();
        }
    }
    
    class B extends Thing
    {
        public function __construct()
        {
            echo "Class B initialized" . PHP_EOL;
            // increment storage size
            ThingsStorage::increment();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题