Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.