Abstract class's inherited class not throwing fatal errors; undefined function calls fatal errors bizarely do

Am I missing some error reporting mode so that this code:

<?php

ini_set('display_errors',1);
error_reporting(E_ALL);

abstract class weapon
 {abstract function fire();}

class sword extends weapon
 {}

echo 'hello world';

?>

would actually report something telling me that the abstract method fire() was not defined in inheriting class?

it presently fails silently not echoing hello world

using php 5.4.24, php.ini have display_errors 0


running it on php 5.3.27, php.ini with display_errors 1, will throw this error:

Fatal error: Class sword contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (weapon::fire) in /public_html/test.php on line 10

there's something funny though

because some fatal error will throw up, like undefined functions:

<?php

ini_set('display_errors',1);
error_reporting(E_ALL);

abstract class weapon
 {abstract function fire();}

class sword extends weapon
 {function fire()
      {}}

handle();

echo 'hello world';

?>

this code will throw Fatal error: Call to undefined function handle() in /test.php on line 13 even though php.ini's display_errors is set to 0...

Upvotes: 1

Views: 257

Answers (1)

Jeremy Harris
Jeremy Harris

Reputation: 24579

You are not seeing the error because your php.ini file has display_errors set to 0. Normally, using ini_set('display_errors',1) will work, except in the case of a fatal error:

Although display_errors may be set at runtime (with ini_set()), it won't have any effect if the script has fatal errors. This is because the desired runtime action does not get executed.

Your script does indeed throw the following error:

Fatal error: Class sword contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (weapon::fire)

I recommend you enable display_errors in your development environment so you can ALWAYS see errors.

Upvotes: 2

Related Questions