[PHP] function and interface in parameter list?
Hi I found a function declaration in an open source project (in php5) and I could not find documantation for this. The function declaration is: public function registerComp(IComp $comp) Is it a kind of typecast? -- Cheers! Marco -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] function and interface in parameter list?
Marco Schuler wrote: Hi I found a function declaration in an open source project (in php5) and I could not find documantation for this. The function declaration is: public function registerComp(IComp $comp) Is it a kind of typecast? no its a type declaration (is that the correct term). it states that $comp must be an object of class IComp (or a subclass of it). if $comp is not anj IComp object you get a fatal error. this only works for classes. and it does not work in conjunction with optional params e.g: public function registerComp(IComp $comp = null) { /**/ } (which IMHO sucks but hey c'est la vie! :-) ) -- Cheers! Marco -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] function and interface in parameter list?
Hi Am Dienstag, 1. Februar 2005 15:50 schrieb Jochem Maas: Marco Schuler wrote: Hi I found a function declaration in an open source project (in php5) and I could not find documantation for this. The function declaration is: public function registerComp(IComp $comp) Is it a kind of typecast? no its a type declaration (is that the correct term). it states that $comp must be an object of class IComp (or a subclass of it). if $comp is not anj IComp object you get a fatal error. Where can I find this in the manual? IComp can also be an interface, right? this only works for classes. and it does not work in conjunction with optional params e.g: public function registerComp(IComp $comp = null) { /**/ } With optional params you mean actually the variable initialisation within the declaration, right? -- Cheers! Marco -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] function and interface in parameter list?
Am Dienstag, 1. Februar 2005 16:07 schrieb Marco Schuler: Am Dienstag, 1. Februar 2005 15:50 schrieb Jochem Maas: Marco Schuler wrote: Hi I found a function declaration in an open source project (in php5) and I could not find documantation for this. The function declaration is: public function registerComp(IComp $comp) Is it a kind of typecast? no its a type declaration (is that the correct term). it states that $comp must be an object of class IComp (or a subclass of it). if $comp is not anj IComp object you get a fatal error. Found it out: It is called Type Hinting. http://ch2.php.net/manual/en/language.oop5.typehinting.php There is a user contributed note on that page: Type hinting allows not to specify a required interface to be supported for the object being passed. Beware, the interpreter will not give an error for that. Is it a bug in php or what is the reason? -- Cheers! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] function and interface in parameter list?
Marco Schuler wrote: Am Dienstag, 1. Februar 2005 16:07 schrieb Marco Schuler: Am Dienstag, 1. Februar 2005 15:50 schrieb Jochem Maas: Marco Schuler wrote: Hi I found a function declaration in an open source project (in php5) and I could not find documantation for this. The function declaration is: public function registerComp(IComp $comp) Is it a kind of typecast? no its a type declaration (is that the correct term). it states that $comp must be an object of class IComp (or a subclass of it). if $comp is not anj IComp object you get a fatal error. Found it out: It is called Type Hinting. ah yes! although ClassType Hinting would be more truthful http://ch2.php.net/manual/en/language.oop5.typehinting.php There is a user contributed note on that page: Type hinting allows not to specify a required interface to be supported for the object being passed. Beware, the interpreter will not give an error for that. Is it a bug in php or what is the reason? I don't even understand what the guy was trying to say in the user comments - let alone if its a bug or a feature! with regard to type hinting with Interfaces - I have no idea offhand, but by using my hands I found out in about 30 seconds... I wrote the bit of code below, if you run it you will find out for yourself whether is works ;-) php -r ' interface MyInt { function doIt(); } class MyObj implements MyInt { function doIt() { echo boo; } } function goGo(MyInt $x) { $x-doIt(); } goGo(new MyObj); ' -- Cheers! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php