php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51421 Abstract __construct constructor argument list not enforced
Submitted: 2010-03-29 01:41 UTC Modified: 2010-06-28 18:41 UTC
From: awood at theiet dot org Assigned: felipe (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2SVN-2010-03-29 (snap) OS: Windows XP SP3
Private report: No CVE-ID: None
 [2010-03-29 01:41 UTC] awood at theiet dot org
Description:
------------
PHP Version = 5.2.6.6

I cannot seem to enforce a class's __construct argument list by defining an abstract constructor in the base class. This can, however, be achieved as expected by using an object interface.

This was mistakenly reported as not working for interfaces either in bug #43557 (closed).

Test script:
---------------
class Type1 {} //for demo purposes
class Type2 {} //for demo purposes

abstract class BaseClass {
   abstract public function __construct(Type1 $foo, Type2 $bar);
}

class ExtendedClass extends BaseClass {
   public function __construct() {
      //expected - fatal error as argument list doesn't match
      //actually happens - nothing, script continues as normal
   }
}

interface SomeInterface {
   public function __construct(Type1 $foo, Type2 $bar);
}

class InterfacedClass implements SomeInterface {
   public function __construct() {
      //generates fatal error as expected
   }
}

Expected result:
----------------
Fatal error when declaring ExtendedClass and InterfacedClass as abstract and concrete method signatures are different.

Actual result:
--------------
Only a fatal error when declaring InterfacedClass, no error when declaring ExtendedClass.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-21 00:11 UTC] felipe@php.net
-Package: Class/Object related +Package: Scripting Engine problem
 [2010-06-27 00:05 UTC] felipe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: felipe
 [2010-06-27 00:05 UTC] felipe@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2010-06-27 00:05 UTC] felipe@php.net
Automatic comment from SVN on behalf of felipe
Revision: http://svn.php.net/viewvc/?view=revision&revision=300770
Log: - Fixed bug #51421 (Abstract __construct constructor argument list not enforced)
 [2010-06-28 18:38 UTC] felipe@php.net
Automatic comment from SVN on behalf of felipe
Revision: http://svn.php.net/viewvc/?view=revision&revision=300817
Log: - Reverted fix for bug #51421
 [2010-06-28 18:41 UTC] felipe@php.net
The fix has been reverted for 5.2 and 5.3 branches, due BC issue.
 [2012-10-26 16:33 UTC] dagguh at gmail dot com
Abstract constructors again.
Learn some basics of OOP.

Why would you EVER want to enforce argument list of a constructor is beyond me.
I bet you are doing something like:
new $iHaveNoClueAboutProgramming($foo, $bar);

Guess what, this code is bad.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 15:01:30 2024 UTC