note 109405 modified in language.oop5.typehinting by danbrown

From: Date: Mon, 16 Jul 2012 15:17:44 +0000
Subject: note 109405 modified in language.oop5.typehinting by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189174@lists.php.net to get a copy of this message
my scalar type hinting solution (example):&#13;&#10;&#60;?php&#13;&#10;class Scalar {&#13;&#10;&#13;&#10; /**&#13;&#10; *&#13;&#10; * @var mixed&#13;&#10; */&#13;&#10; private $value;&#13;&#10;&#13;&#10; /**&#13;&#10; *&#13;&#10; * @param mixed $v&#13;&#10; */&#13;&#10; public function __construct($v) {&#13;&#10; $this-&#62;value = $v;&#13;&#10; }&#13;&#10;&#13;&#10; /**&#13;&#10; *&#13;&#10; * @return string&#13;&#10; */&#13;&#10; public function __toString() {&#13;&#10; $v = strval($this-&#62;value);&#13;&#10; return $v;&#13;&#10; }&#13;&#10;&#13;&#10; /**&#13;&#10; *&#13;&#10; * @return bool&#13;&#10; */&#13;&#10; public function toBool() {&#13;&#10; $v = (bool) $this-&#62;value;&#13;&#10; return $v;&#13;&#10; }&#13;&#10;&#13;&#10; /**&#13;&#10; *&#13;&#10; * @return float&#13;&#10; */&#13;&#10; public function toFloat() {&#13;&#10; $v = floatval($this-&#62;value);&#13;&#10; return $v;&#13;&#10; }&#13;&#10;&#13;&#10; /**&#13;&#10; *&#13;&#10; * @return int&#13;&#10; */&#13;&#10; public function toInt() {&#13;&#10; $v = intval($this-&#62;value);&#13;&#10; return $v;&#13;&#10; }&#13;&#10;&#13;&#10; /**&#13;&#10; *&#13;&#10; * @return string&#13;&#10; */&#13;&#10; public function toString() {&#13;&#10; $v = strval($this-&#62;value);&#13;&#10; return $v;&#13;&#10; }&#13;&#10;&#13;&#10;}&#13;&#10;&#13;&#10;class String extends Scalar {&#13;&#10;&#13;&#10; /**&#13;&#10; *&#13;&#10; * @param string $v&#13;&#10; */&#13;&#10; public function __construct($v) {&#13;&#10; if (is_string($v)) {&#13;&#10; parent::__construct($v);&#13;&#10; } else {&#13;&#10; throw new InvalidArgumentException();&#13;&#10; }&#13;&#10; }&#13;&#10;&#13;&#10; /**&#13;&#10; *&#13;&#10; * @param mixed $v&#13;&#10; * @return String&#13;&#10; */&#13;&#10; public static function parse($v) {&#13;&#10; $v = strval($v);&#13;&#10; return new String($v);&#13;&#10; }&#13;&#10;&#13;&#10;}&#13;&#10;&#13;&#10;/**&#13;&#10; *&#13;&#10; * @param string $s&#13;&#10; */&#13;&#10;function myFunction($s) {&#13;&#10; new String($s); // now $s must be a string to continue&#13;&#10; //...&#13;&#10;}&#13;&#10;?&#62; --was-- my scalar type hinting solution (example): <?php class Scalar { /** * * @var mixed */ private $value; /** * * @param mixed $v */ public function __construct($v) { $this->value = $v; } /** * * @return string */ public function __toString() { $v = strval($this->value); return $v; } /** * * @return bool */ public function toBool() { $v = (bool) $this->value; return $v; } /** * * @return float */ public function toFloat() { $v = floatval($this->value); return $v; } /** * * @return int */ public function toInt() { $v = intval($this->value); return $v; } /** * * @return string */ public function toString() { $v = strval($this->value); return $v; } } class String extends Scalar { /** * * @param string $v */ public function __construct($v) { if (is_string($v)) { parent::__construct($v); } else { throw new InvalidArgumentException(); } } /** * * @param mixed $v * @return String */ public static function parse($v) { $v = strval($v); return new String($v); } } /** * * @param string $s */ function myFunction($s) { new String($s); // now $s must be a string to continue ... } Code could be found here: https://code.google.com/p/myquicknet/source/browse/src/php_class/com/googlecode/myquicknet/scalar/ http://php.net/manual/en/language.oop5.typehinting.php

« previous php.notes (#189174) next »