php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #43274 __destruct 2
Submitted: 2007-11-13 07:36 UTC Modified: 2010-12-23 07:40 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: miha dot vrhovnik at domenca dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.5 OS: Linux
Private report: No CVE-ID: None
 [2007-11-13 07:36 UTC] miha dot vrhovnik at domenca dot com
Description:
------------
Followup to #43264 as i cannot add another comment there :(
__constructor was a typo :( (Old habits die hard.)(I've seen that then it also dies in constructor if there is no base class)

Why each class should have __destruct, __construct function let's take another look from another perspective.

I take external class from the web e.g. Smarty
I extend Smarty class, but I need class destructor. As currently Smarty doesn't have destructor I don't write parent:__destruct into my destructor. Then Smarty guys decide they need it and somebody at my company decides that it will upgrade Smarty to that version. In most cases we just introduced a difficult to find bug.

After making some more research. The same applies if I comment out a __constuct in base class. Not calling parent::__construct in extended class if base class gets constructor might result in difficult to find bugs.

So I beleive that php should provide parent class constructor/destructor if it doesn't have it already. Well or at least this shouldn't be a fatal error.

Reproduce code:
---------------
error_reporting(E_ALL);

$c = new extender();

class base
{
    public function __construct()
    {
        //parent::__construct();
    }
    
}

class extender extends base
{
    public function __construct()
    {
        parent::__construct();
    }
    
    
    public function __destruct()
    {
        parent::__destruct();
    }
}

Expected result:
----------------
Not exiting with fatal error. e.g provide default constructor/destructor functions for every class.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-22 13:40 UTC] johannes@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-12-22 13:40 UTC] johannes@php.net
If the parent class changes its interface you have to adopt. That happens not only with __destruct() but with other hanges, too. What about if they add a parameter such?
 [2010-12-23 07:40 UTC] miha dot vrhovnik at domenca dot com
If a parameter is added and they are not optional I would consider this a fatal error. Or warning could do just fine just like when normal method signature changes. Although I would consider not giving all required params to a function a fatal error...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 26 12:01:29 2024 UTC