Introduction

This extension aims at helping people making PHP a stronger typed language and can be a good alternative to scalar type hinting. It provides different typehandling classes as such as integer, float, bool, enum and string

Warning

This extension is EXPERIMENTAL. The behaviour of this extension including the names of its functions and any other documentation surrounding this extension may change without notice in a future release of PHP. This extension should be used at your own risk.

Коментарии

Автор:
For backwards compatibility issues:
We do know that auto-unboxing works for strings, thanks to the __toString() magic function. Auto-unboxing for other scalar types may be tricky but possible, by using the same function with another return type (which of cause has some limitations).

However auto-boxing is a real challenge.
If you can provide any details, I would appreciate that.

For auto-unboxing see this code-fragment:

<?php
class MyString {
  private 
$value "";
  public function 
__construct($string) {
    if (!
is_string($string)) {
      throw new 
InvalidArgumentException();
    }
   
$this->value $string;
  }
  public function 
__toString() {
    return 
$this->value;
  }
}
?>

For other scalar types, all that changes is the constructor function. Anything else remains unchanged. (While you are at it, you may want to add some type conversion functions for your personal convenience.)

HowTo:

<?php
function foo(MyString $string)
{
  print 
"$string"// auto-unboxing in action
}

// prints "Hello World!"
foo(new MyString("Hello World!"));
?>
2009-10-28 10:09:43
http://php5.kiev.ua/manual/ru/intro.spl-types.html
Автор:
For your information, here is another work-around for this issue, that even works with PHP4 (in case you need to be compatible):

<?php
/**
 * @param string $foo
 */
function foo($foo)
{
 
assert('is_string($foo); // Invalid argument type');
 
// do some foo
}
?>

This single-line of code should fix it for most people. It will throw an error whenever the expression evaluates to false (if $foo is not a string).

Most errors concerning scalar types are found during development or unit-testing. PHPUnit is fine with these and reports failed assertions as failed tests, also presenting the included message.
Note that assertions need to be turned on for this to work. See the manual page on assertions for more details.
Note that you may deactivate the checks on the target machine, so this comes without any performance penalty. So don't hesitate to make extensive use of this PHP-feature.

Note though: DON'T RELY ON ASSERTIONS FOR CHECKING USER INPUT!

In that case you may want to try this code:
is_string($foo) || trigger_error('String expected', E_USER_ERROR);

Or: simply throw an exception (as usual).
2009-10-28 10:34:57
http://php5.kiev.ua/manual/ru/intro.spl-types.html
abstract class ContainerType{
    protected $value;
    protected $length;
    protected $isRealContainer;
    abstract protected function get_length();
    abstract protected function is_real_container();
    abstract protected function init();
}

class JLNumber extends ContainerType{
    public $value;
    public $length;
    public $isRealContainer;

    public function __construct($val){
        $this->value     = $val;
        $this->length     = 0;
        $this->isRealContainer = FALSE;
        return $this->init();
    }
    protected function get_length(){
        $this->length = strlen(trim($this->value));
        return $this;
    }
    protected function is_real_container(){
        $this->isRealContainer = is_numeric($this->value);
        return $this->isRealContainer;
    }

    protected function init(){
        if($this->is_real_container()){
            $this->get_length();
        }else{
            $this->value="";
        }
        return $this;
    }

    public function __toString(){
        $real = ($this->isRealContainer)?"TRUE":"FALSE";
        $str = "<pre>";
        $str .= "Class Name : ".get_class()."<br>";
        $str .= "Type : Number<br>";
        $str .= "Value : ".$this->value."<br>";
        $str .= "Length : ".$this->length."<br>";
        $str .= "Is Real Number : ".$real."<br><br>";
        $str .= "</pre>";
        return $str;
    }
}
2016-08-31 17:25:08
http://php5.kiev.ua/manual/ru/intro.spl-types.html

    Поддержать сайт на родительском проекте КГБ