note 117907 added to language.oop5.typehinting
| From: | Anonymous | Date: | Sun, 30 Aug 2015 13:52:16 +0000 |
| Subject: | note 117907 added to language.oop5.typehinting | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203522@lists.php.net to get a copy of this message | ||
i think this is pretty close.
$lines=hhb_mustbe('array',file("foofile"));
//either file succeeds, and returns an array, or file returns FALSE which is not an array, which
throws an unexpectedValueException.
$socket=hhb_mustbe('Socket',socket_create(AF_INET,SOCK_STREAM,getprotobyname('tcp')));
//either socket_create succeeds, and returns a Socket, or socket_create returns False, which is not
a resource of type Socket, and you get an UnexpectedValueException
$size=hhb_mustbe('int',filesize(somefile));
//either filesize() returns an integer, or returns FALSE wich is not an int, and you'll get
UnexpectedValueException.
function hhb_mustbe(/*string*/$type,/*mixed*/$variable){
//should it be UnexpectedValueException or InvalidArgumentException?
//going with UnexpectedValueException for now...
$actual_type=gettype($variable);
if($actual_type==='unknown type'){
//i dont know how this can happen, but it is documented as a possible return value of gettype...
throw new Exception('could not determine the type of the variable!');
}
if($actual_type==='object'){
if(!is_a($variable,$type)){
$dbg=get_class($variable);
throw new UnexpectedValueException('variable is an object which does NOT implement class:
'.$type.'. it is of class: '.$dbg);
}
return $variable;
}
if($actual_type==='resource'){
$dbg=get_resource_type($variable);
if($dbg!==$type){
throw new UnexpectedValueException('variable is a resource, which is NOT of type:
'.$type.'. it is of type: '.$dbg);
}
return $variable;
}
//now a few special cases
if($type==='bool'){
$parsed_type='boolean';
} else if($type==='int'){
$parsed_type='integer';
} else if($type==='float'){
$parsed_type='double';
} else if($type==='null'){
$parsed_type='NULL';
} else{
$parsed_type=$type;
}
if($parsed_type!==$actual_type && $type!==$actual_type){
throw new UnexpectedValueException('variable is NOT of type: '.$type.'. it is of
type: '.$actual_type);
}
//ok, variable passed all tests.
return $variable;
}
----
Server IP: 72.52.91.14
Probable Submitter: 85.164.166.109
----
Manual Page -- http://php.net/manual/en/language.oop5.typehinting.php
Edit -- https://master.php.net/note/edit/117907
Del: integrated -- https://master.php.net/note/delete/117907/integrated
Del: useless -- https://master.php.net/note/delete/117907/useless
Del: bad code -- https://master.php.net/note/delete/117907/bad+code
Del: spam -- https://master.php.net/note/delete/117907/spam
Del: non-english -- https://master.php.net/note/delete/117907/non-english
Del: in docs -- https://master.php.net/note/delete/117907/in+docs
Del: other reasons-- https://master.php.net/note/delete/117907
Reject -- https://master.php.net/note/reject/117907
Search -- https://master.php.net/manage/user-notes.php