dongneng5383 2014-01-30 16:06
浏览 218
已采纳

将析构函数声明为私有是否可以?

Some of my classes declare their constructors as private because an object of such class is only allowed to be created by a static method of the class. May I also declare destructors of such classes as private to keep it symmetric, is it safe?

EDIT: Ok, seems like this is simply not possible:

Fatal error: Call to private AClass::__destruct() from context '' in /script on line 0

(the context is empty and there's no such thing as line 0). For some reason I used to think that the PHP runtime is almighty and can destruct anything it wants.

  • 写回答

2条回答 默认 最新

  • dongxingchang9345 2014-01-30 16:21
    关注

    In php the __destruct magic method must be public. The method will automatically be called externally to the instance. Declaring __destruct as protected or private will result in a warning and the magic method will not be called.

    There's no symmetry necessary, as you should never explicitly call __destruct.

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

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿