On Nov 19, 2007 12:13 AM, David Coallier <[EMAIL PROTECTED]> wrote:
> I was thinking at something along the lines of objects also for instance:
>
> $i = new Integer(33);
>
> function foo(Integer $var) {
> }
>
> foo ($i); else it emits a fatal error. But also if you do
>
> $i = "Name"; that would emit a fatal error because the value is
> suposed to be an int. This might look a bit too much like java, but as
> an extension it could be something quite interesting I believe.
>
> String, Object, Integer, Scalar, Float  and what else.
>
> So thinking of something like
>
> $string = new String("Foo");
> $string = "bar" or $string->setValue("Bar"); would do
>
> $float = new Float(4.242);
> $float->setValue('foobar'); // That emits an error
> $float->setValue(3.14159);
>
> echo $float; (__toString) or echo $float->getValue; to echo it's content/value
>
> and so on.

That has got to be the worst idea I've heard on internals for over a month.
Besides, you can do this in userland already anyway:

<?php
class InvalidTypeException extends Exception {}
class UnknownTypeException extends Exception {}

class Types {
        const INTEGER = 1;
        const FLOAT   = 2;
        const STRING  = 3;
        const OBJECT  = 4;
        const BOOLEAN = 5;

        private $val, $type;

        public function __construct($val, $type) {
                $this->type = $type;
                $this->setValue($val);
        }
        public function setValue($val) {
                switch($this->type) {
                case self::INTEGER:
                        if (!is_int($val)) {
                                throw new InvalidTypeException;
                        }
                        break;

                case self::FLOAT:
                        if (!is_float($val)) {
                                throw new InvalidTypeException;
                        }
                        break;

                case self::STRING:
                        if (!is_string($val)) {
                                throw new InvalidTypeException;
                        }
                        break;

                case self::OBJECT:
                        if (!is_object($val)) {
                                throw new InvalidTypeException;
                        }
                        break;

                case self::BOOLEAN:
                        if (!is_bool($val)) {
                                throw new InvalidTypeException;
                        }
                        break;

                default:
                        throw new UnknownTypeException;

                }
                $this->val = $val;
        }
        public function getValue() {
                return $this->val;
        }
        public function __toString() {
                return (string)$this->getValue();
        }
}

class Integer extends Types {
        public function __construct($val) {
                parent::__construct($val, Types::INTEGER);
        }
}
class String extends Types {
        public function __construct($val) {
                parent::__construct($val, Types::STRING);
        }
}
class Float extends Types {
        public function __construct($val) {
                parent::__construct($val, Types::FLOAT);
        }
}
class Object extends Types {
        public function __construct($val) {
                parent::__construct($val, Types::OBJECT);
        }
}
class Boolean extends Types {
        public function __construct($val) {
                parent::__construct($val, Types::BOOLEAN);
        }
}
function type_hint_integer(Integer $val) {
        echo $val, "\n";
}
function type_hint_string(String $val) {
        echo $val, "\n";
}
function type_hint_float(Float $val) {
        echo $val, "\n";
}



type_hint_integer(new Integer(123));
type_hint_string(new String("string"));
type_hint_float(new Float(0.25));

-Hannes

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to