marko.crni Posted December 23, 2009 Share Posted December 23, 2009 I have problem with type-hinting and extending tidy. This code creates error: class cMyTidy extends tidy { public $tralala; } $oMyTidy = new cMyTidy(); doSomething($oMyTidy); function doSomething(cMyTidy $oObject) { var_dump($oObject); } PHP Fatal error: Argument 1 passed to doSomething() must be an instance of cMyTidy...... If I change type-hinting in function from cMyTidy to tidy everything is OK. Where is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/186127-extending-tidy/ Share on other sites More sharing options...
trq Posted December 23, 2009 Share Posted December 23, 2009 The problem is that the cMyTidy is of type tidy because it extends it. Placing tidy within the type hint is correct. Quote Link to comment https://forums.phpfreaks.com/topic/186127-extending-tidy/#findComment-982953 Share on other sites More sharing options...
Daniel0 Posted December 23, 2009 Share Posted December 23, 2009 This is a bug in PHP itself apparently. This code: <?php class cMyTidy extends tidy { // foo } function doSomething(cMyTidy $o) { var_dump($o); } $o = new cMyTidy(); var_dump(is_a($o, get_class($o)), get_class($o)); doSomething(new cMyTidy()); results in bool(false) string(7) "cMyTidy" PHP Catchable fatal error: Argument 1 passed to doSomething() must be an instance of cMyTidy, instance of tidy given, called in /home/daniel/test.php on line 15 and defined in /home/daniel/test.php on line 7 Catchable fatal error: Argument 1 passed to doSomething() must be an instance of cMyTidy, instance of tidy given, called in /home/daniel/test.php on line 15 and defined in /home/daniel/test.php on line 7 That is obviously an error. An object is always an instance of its own class. This code: <?php class foo {} class bar extends foo {} $foo = new foo(); $bar = new bar(); var_dump( get_class($foo), is_a($foo, get_class($foo)), get_class($bar), is_a($bar, get_class($bar)) ); results in string(3) "foo" bool(true) string(3) "bar" bool(true) as expected. Quote Link to comment https://forums.phpfreaks.com/topic/186127-extending-tidy/#findComment-982966 Share on other sites More sharing options...
marko.crni Posted December 23, 2009 Author Share Posted December 23, 2009 @thrope Daniel0 is right Here is another weird thing class cMyTidy extends tidy { public $tralala; } $oMyTidy = new cMyTidy(); doSomething($oMyTidy); function doSomething($oObject) { var_dump($oObject); // returns object(cMyTidy)#1 ..... var_dump($oObject instanceof cMyTidy); // returns bool(false) } Quote Link to comment https://forums.phpfreaks.com/topic/186127-extending-tidy/#findComment-982991 Share on other sites More sharing options...
Daniel0 Posted December 23, 2009 Share Posted December 23, 2009 I've filed a bug report here: http://bugs.php.net/bug.php?id=50558 Feel free to comment on it. instanceof works like is_a, so it would fail if is_a() does. Quote Link to comment https://forums.phpfreaks.com/topic/186127-extending-tidy/#findComment-982995 Share on other sites More sharing options...
trq Posted December 23, 2009 Share Posted December 23, 2009 Yeah I guess it is a bug. I always thought it strange, but never really looked too deeply into it. Quote Link to comment https://forums.phpfreaks.com/topic/186127-extending-tidy/#findComment-983015 Share on other sites More sharing options...
Daniel0 Posted December 25, 2009 Share Posted December 25, 2009 The bug has been fixed in SVN. I think it should be available in the next release of 5.2 and 5.3. Quote Link to comment https://forums.phpfreaks.com/topic/186127-extending-tidy/#findComment-983949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.