Did you know you can do this?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

i suck lolol oh well
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The only primitive that has typehinting support beyond objects is array, and that was added in 5.1, if memory serves.
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post 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;
}

?>
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

because that way he only writes it once rather than twice?
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post by julian_lp »

Daedalus- wrote:because that way he only writes it once rather than twice?
the best of the reasons indeed :wink:
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post 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
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

My mistake :(
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post by julian_lp »

Daedalus- wrote:My mistake :(
Errare humanum est :wink: :wink: :wink: :wink:
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
Post Reply