Did you know you can do this?
Moderator: General Moderators
- julian_lp
- Forum Contributor
- Posts: 121
- Joined: Sun Jul 09, 2006 1:00 am
- Location: la plata - argentina
onion2k wrote:If, like me, you're forced to maintain PHP4 code, you can sort of do the same with..
Quite handy for library classes.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; } ?>
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;
}
?>- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
- julian_lp
- Forum Contributor
- Posts: 121
- Joined: Sun Jul 09, 2006 1:00 am
- Location: la plata - argentina
Yes:feyd wrote:The only primitive that has typehinting support beyond objects is array, and that was added in 5.1, if memory serves.
"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
Better yet..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; } ?>
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;
}
}
?>