php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #28269 Incomplete error message when instantiating child classes of an abstract class
Submitted: 2004-05-04 03:43 UTC Modified: 2006-04-03 12:46 UTC
Votes:5
Avg. Score:5.0 ± 0.0
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:4 (80.0%)
From: phpbug at gb404 dot com Assigned: helly (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 5.0.2 OS: *
Private report: No CVE-ID: None
 [2004-05-04 03:43 UTC] phpbug at gb404 dot com
Description:
------------
When I try to instantiate a class which extends an abstract class but without implemented all defined abstract functions, this class becomes abstract too and fatal error message returned doesn't give any reason.

Then it is very difficult to find which abstract function is not implemented as defined in the abstract parent class when there is a lot of abstract functions to implement.

Is it possible to display a list of the missing function after the actual error message ?

Reproduce code:
---------------
<?php
	abstract class a
	{
		abstract function foo();
		abstract function bar();
	}

	class b extends a
	{
		function foo() {
			return "I am the foo function";
		}
	}

	$obj = new b();
?>

Expected result:
----------------
Fatal error: Cannot instantiate class b, abstract function parent::bar not implemented in /absolute/path/to/my/file/b.php on line 15

Actual result:
--------------
Fatal error: Cannot instantiate abstract class b in /absolute/path/to/my/file/b.php on line 15

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-04 05:44 UTC] phpbug at gb404 dot com
This is not specific to linux
 [2004-05-19 14:27 UTC] cunha at gabcmt dot eb dot mil dot br
I'm experiencing the same problem. It's very difficult to discover the missing method if PHP doesn't give any hint. 

The message given for the first class that "implements" some interface is complete and has IMHO all necessary information to the programmer. But the message given for any subclasses is generic and doesn't tell which methods are missing and why.
 [2004-09-24 17:46 UTC] bobalong at gmx dot net
IMO it's not really a problem with instantiation. The perfect solution would be if a compile-time error was thrown asking the developer to implement the missing method(s), or declare the class abstract, just as is done when partially implementing an interface in a class that is not declared abstract.

As a side note on this issue, another nice feature that would make a lot of PHP5 OO code more readable and intuitave would be the allowance by the parser for the re-declaration of abstract functions. At the moment the usual "cannot re-dfeine function" message gets thrown. This just needs to ignore abstract functions (in abstract classes), i.e.

interface a
{
	function foo();
}

abstract class b implements a
{
	abstract function foo();
}

class c extends b
{
	function foo()
	{
		// do something...
	}
}
 [2004-09-25 00:21 UTC] helly@php.net
Abstract redefinition is a feature request - the rest was fixed in 5.0.0
 [2004-09-25 00:22 UTC] helly@php.net
Abstract redefinition is a feature request - the rest was fixed in 5.0.0
 [2004-09-25 20:16 UTC] bobalong at gmx dot net
Cool, and I'm sure that's not at the top of the priority list. However, in the example above, if foo() is not implemented in class c, no compile error is thrown. The developer must instantiate the class prior to knowing it is abstract. This forces the developer to manually ensure that all abstract members from higher up in an inheritence heirarchy have been implemented.

Is this intentional behaviour?
 [2004-09-25 20:16 UTC] bobalong at gmx dot net
Cool, and I'm sure that's not at the top of the priority list. However, in the example above, if foo() is not implemented in class c, no compile error is thrown. The developer must instantiate the class prior to knowing it is abstract. This forces the developer to manually ensure that all abstract members from higher up in an inheritence heirarchy have been implemented.
 [2004-09-26 20:39 UTC] helly@php.net
Your last complain is fixed too:

marcus@frodo /usr/src/PHP_5_0 $ php -r 'interface foo{function bar();} class baz implements foo{}'
make: `sapi/cli/php' is up to date.

Fatal error: Class baz contains 1 abstract methods and must therefore be declared abstract (foo::bar) in Command line code on line 1
marcus@frodo /usr/src/PHP_5_0 $ php -v
make: `sapi/cli/php' is up to date.
PHP 5.0.3-dev (cli) (built: Sep 25 2004 00:37:01) (DEBUG)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.2-dev, Copyright (c) 1998-2004 Zend Technologies
 [2004-09-27 10:37 UTC] bobalong at gmx dot net
Thank you! Keep up the great work! PHP5 is awsome.
 [2006-04-03 12:46 UTC] tony2001@php.net
Fixed -> closed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 10:01:28 2024 UTC