Jump to content

extending tidy


marko.crni

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/186127-extending-tidy/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/186127-extending-tidy/#findComment-982966
Share on other sites

@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)

}

Link to comment
https://forums.phpfreaks.com/topic/186127-extending-tidy/#findComment-982991
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.