Jump to content

Can i somhow limit the function by definition?


sangoku

Recommended Posts

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

 

 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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) {}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.