jaikar Posted September 26, 2010 Share Posted September 26, 2010 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 https://forums.phpfreaks.com/topic/214466-argument-type-declation-and-passing-same-type-returns-error/ Share on other sites More sharing options...
rwwd Posted September 26, 2010 Share Posted September 26, 2010 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 https://forums.phpfreaks.com/topic/214466-argument-type-declation-and-passing-same-type-returns-error/#findComment-1116014 Share on other sites More sharing options...
jaikar Posted September 26, 2010 Author Share Posted September 26, 2010 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 https://forums.phpfreaks.com/topic/214466-argument-type-declation-and-passing-same-type-returns-error/#findComment-1116019 Share on other sites More sharing options...
jaikar Posted September 26, 2010 Author Share Posted September 26, 2010 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 https://forums.phpfreaks.com/topic/214466-argument-type-declation-and-passing-same-type-returns-error/#findComment-1116027 Share on other sites More sharing options...
rwwd Posted September 26, 2010 Share Posted September 26, 2010 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 https://forums.phpfreaks.com/topic/214466-argument-type-declation-and-passing-same-type-returns-error/#findComment-1116031 Share on other sites More sharing options...
jaikar Posted September 26, 2010 Author Share Posted September 26, 2010 oh ok!.. thankyou!.. is "Array" is a class in php or data type ? and is array different from Array? Link to comment https://forums.phpfreaks.com/topic/214466-argument-type-declation-and-passing-same-type-returns-error/#findComment-1116033 Share on other sites More sharing options...
rwwd Posted September 26, 2010 Share Posted September 26, 2010 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 https://forums.phpfreaks.com/topic/214466-argument-type-declation-and-passing-same-type-returns-error/#findComment-1116050 Share on other sites More sharing options...
jaikar Posted September 27, 2010 Author Share Posted September 27, 2010 rwwd, thankyou so much for your time and inputs!.. highly appreciate it.. i would like to add you to my buddy list, also would like to discuss on some best php practices, are you interested? Link to comment https://forums.phpfreaks.com/topic/214466-argument-type-declation-and-passing-same-type-returns-error/#findComment-1116279 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.