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

 

 

  Quote

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.