dongyuxiao6295 2014-10-07 09:34
浏览 16

php - 自动加载和自定义错误处理程序

I've registered an autoloader (spl_autoload_register) and set a custom error handler (set_error_handler).

Next I have 3 classes: Class1, Class2 and Class3. Class2 extends Class1 but their methods getText() do not match. (Class1 expects a parameter of type Class3)

Including Class1 first and Class2 second gives me a fatal error "Class 'Class3' not found in ...". What I would expect is a strict warning. (If I do not include Class1 first everything is fine.)

index.php

<?php
class AutoLoader
{
  public static function run ($className)
  {
    require(__DIR__."/".$className.".php");
  }
}
spl_autoload_register(array('AutoLoader', "run"));

class ErrorHandler
{
  public static function error ($errno, $errstr, $errfile, $errline)
  {
    echo 'error';
  }
}
set_error_handler('ErrorHandler::error');

echo 'a';
require( __DIR__.'\Class1.php' );
echo 'b';
require( __DIR__.'\Class2.php' );
echo 'c';
?>

Class1.php

<?php
class Class1
{
  public static function getText ($text=Class3::TEXT)
  {
  }
}
?>

Class2.php

<?php
class Class2 extends Class1
{
  public static function getText ()
  {
  }
}
?>

Class3.php

<?php
class Class3
{
  const TEXT = "text";
}
?>

With php 5.3.5 I get the expected result: aberrorc

With php 5.5.6 I get the fatal error: abFatal error...

A similar problem is discribed here set_error_handler function not calling autoload and here https://bugs.php.net/bug.php?id=54054 (Both try to autoload a class within the error-handler-function. The answers are about "compiling/execution", saying it is "not a bug". I'm in doubt about this, cause it's working in 5.3 and it's not exactly the same setting.)

How can I fix this issue without sorting out all strict-warnings in my scripts?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。