Jump to content

argument type declation and passing same type returns error


jaikar

Recommended Posts

hi there,

 

i am fairly new to OOPs in php, i get an error when i declare the argument type (as object) in a function and pass the same type (object).

 

 

class eBlast {
       public static function getEmail(object $result) {
           return $result->email;
       }
}

$r = mysql_fetch_object($query);
eBlast::getEmail($r);

echo gettype($r); // outputs: object

 

error is :

 

Catchable fatal error: Argument 1 passed to eBlast::getEmail() must be an instance of object, instance of stdClass given, called in C:\wamp\www\integra\client\pl_eblast\admin\send_emails.php on line 145 and defined in C:\wamp\www\integra\client\pl_eblast\app\app.eBlast.php on line 8

 

if i remove the type declaration in the function it works, but just would like to know why it shows error when pass the same type, also isnt mysql_fetch_object is the instance of stdclass?

 

thanks in advance!

 

 

Link to comment
Share on other sites

Your calling that using the scope resolution operator (::) I am just wondering what level of error_reporting you have on?

 

Having the public static declaration is correct, so is there anything being passed into the _object($query); there, ie what is in the var your passing? This doesn't sound right to me... It shouldn't give an error I don't think..

 

Rw

Link to comment
Share on other sites

thanks for the reply!

well, error_reporting is default, i did not changed it. also it say its a fatal_error, which seems an important error?

 

i did not passed any var as the second parameter, but i just tested by passing "stdClass" as a second param, but it still returns an error, if possible can you test on your machine and change the query according your database if its handy and let me know what happens, so i can know if any other problem on my server side?..

 

thankyou!

Link to comment
Share on other sites

hey!, it works now if i substitute the arg type to stdClass instead of object like

 

function getEmail(stdClass $result)

 

ok, so i should not declare the type, but have to use the class name instead of the type name ... but i still have to use type name in case of array?.. quite confusing here...  have to test more ...  please let me know if you know more on this .. thankyou!

Link to comment
Share on other sites

public static function getEmail(stdClass $result, Array $YourArray) 

 

Having it like that should generate an error now if you pass anything OTHER that an array because using Array will invoke the error if a string is passed by mistake.

 

Have fun.

 

Rw

Link to comment
Share on other sites

function(Array $PassedArray){
//your processes
}

 

This means that the data being passed into the function HAS to be an array, OR the parser will alert you by throwing an error saying "Invalid data, expected Array, string/boolen given", or something to the effect of anyway. But if memory serves, separating the types by comma divides them into parameters, though that is basic php; and I'm way late for bed!

 

Though I must admit, I have not had to use 'stdClass' in a function yet, though when you think about it, it's plausible, though there may be another way to declare it...

 

Rw

Link to comment
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.