sangoku Posted May 31, 2010 Share Posted May 31, 2010 Hy i usually limit the variable when i pass a class by a variable like protected function (class_name $class){ some crunching } So i limit the variable to only a class of that name.... My question is: can i somehow limit on same way a variable to be only int or array or string or somthing else... side note i know how to check a variable if it any of given cind of data... i am asking is it possible to do it the way it looks soo pretty XD Quote Link to comment https://forums.phpfreaks.com/topic/203444-can-i-somhow-limit-the-function-by-definition/ Share on other sites More sharing options...
premiso Posted May 31, 2010 Share Posted May 31, 2010 As far as I know this should also work: function (int $i) And it will do the check for you. If a non-int is passed in it should throw a warning or an exception. Only way to find out is to try it out Quote Link to comment https://forums.phpfreaks.com/topic/203444-can-i-somhow-limit-the-function-by-definition/#findComment-1065822 Share on other sites More sharing options...
sangoku Posted May 31, 2010 Author Share Posted May 31, 2010 know about int ... string tough thinks it is a class called string.... QQ Quote Link to comment https://forums.phpfreaks.com/topic/203444-can-i-somhow-limit-the-function-by-definition/#findComment-1065826 Share on other sites More sharing options...
Daniel0 Posted May 31, 2010 Share Posted May 31, 2010 PHP doesn't support scalar type hinting. Currently there are plans for implementing scalar type hints in a future version of PHP, but exactly how it's going to work hasn't been decided yet. Quote Link to comment https://forums.phpfreaks.com/topic/203444-can-i-somhow-limit-the-function-by-definition/#findComment-1065829 Share on other sites More sharing options...
ignace Posted May 31, 2010 Share Posted May 31, 2010 PHP doesn't support scalar type hinting. Currently there are plans for implementing scalar type hints in a future version of PHP, but exactly how it's going to work hasn't been decided yet. You can also go the Java way of course, for example: class Integer { private $value = 0; public function __construct($value) { $this->value = intval($value); } public static function parseInt($string) { return intval($string); } } $int = new Integer(5); $intie = Integer::parseInt($_GET['value']); class MyClass { public function __construct(Integer $int) {} } Quote Link to comment https://forums.phpfreaks.com/topic/203444-can-i-somhow-limit-the-function-by-definition/#findComment-1065876 Share on other sites More sharing options...
sangoku Posted June 1, 2010 Author Share Posted June 1, 2010 QQ me like this way of coding looks sooooooo pretttyyy Q.Q Quote Link to comment https://forums.phpfreaks.com/topic/203444-can-i-somhow-limit-the-function-by-definition/#findComment-1066168 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.