Page 3 of 3

Posted: Wed Jul 26, 2006 5:00 pm
by daedalus__
i suck lolol oh well

Posted: Wed Jul 26, 2006 5:39 pm
by feyd
The only primitive that has typehinting support beyond objects is array, and that was added in 5.1, if memory serves.

Posted: Wed Jul 26, 2006 10:21 pm
by julian_lp
onion2k wrote:If, like me, you're forced to maintain PHP4 code, you can sort of do the same with..

Code: Select all

<?php

function php4TypeHinting($var) {
	$type = gettype($var);
	if ($type=="resource") 	{ $type = get_resource_type($var); }
	if ($type=="object") 	{ $type = get_class($var); }
	return $type;
}

?>
Quite handy for library classes.

kool, but I wonder why you didnt write the return after the first if...

Code: Select all

<?php

function php4TypeHinting($var) {
	$type = gettype($var);
	if ($type=="resource") 	{ $type = get_resource_type($var);  ***return $type;}
	if ($type=="object") 	{ $type = get_class($var); }
	return $type;
}

?>

Posted: Wed Jul 26, 2006 10:28 pm
by daedalus__
because that way he only writes it once rather than twice?

Posted: Wed Jul 26, 2006 10:34 pm
by julian_lp
Daedalus- wrote:because that way he only writes it once rather than twice?
the best of the reasons indeed :wink:

Posted: Thu Jul 27, 2006 1:22 am
by julian_lp
feyd wrote:The only primitive that has typehinting support beyond objects is array, and that was added in 5.1, if memory serves.
Yes:

"Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported."

see
http://ch2.php.net/language.oop5.typehinting

Sadly, from previous post I get convinced that type hinting int-string was possible :( I must forget about it

Posted: Thu Jul 27, 2006 1:32 am
by daedalus__
My mistake :(

Posted: Thu Jul 27, 2006 1:36 am
by julian_lp
Daedalus- wrote:My mistake :(
Errare humanum est :wink: :wink: :wink: :wink:

Posted: Thu Jul 27, 2006 3:13 am
by onion2k
julian_lp wrote:kool, but I wonder why you didnt write the return after the first if...

Code: Select all

<?php

function php4TypeHinting($var) {
	$type = gettype($var);
	if ($type=="resource") 	{ $type = get_resource_type($var);  ***return $type;}
	if ($type=="object") 	{ $type = get_class($var); }
	return $type;
}

?>
Better yet..

Code: Select all

<?php
function php4TypeHinting($var) {
	switch ($type = gettype($var)) {
		case "resource": return get_resource_type($var); break;
		case "object": return get_class($var); break;
		default: return $type; break;
	}
}
?>
The breaks aren't actually necessary, but I hate leaving them out.