note 103463 added to language.oop5.typehinting
| From: | Anonymous at osu1 dot php dot net | Date: | Thu, 14 Apr 2011 14:43:46 +0000 |
| Subject: | note 103463 added to language.oop5.typehinting | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178830@lists.php.net to get a copy of this message | ||
Please note, there is a strong typing in PHP.
It's fully described in this article: http://php.webtutor.pl/?p=810
How does it work? You have to import one PHP library to your existing projects, and create some
classes, in which the scalar values will be automatically wrapped by an autoboxing mechanism known
from Java, like so:
<?php
class String extends AutoBoxedObject
{
public $value;
public function __construct($value) {
$this->value = $value;
}
public function __toString() {
return "$this->value";
}
public function toUpperCase() {
return strtoupper($this->value);
}
}
function & string($value = null) {
$x = & VariablesManager::getNewPointer(new String($value));
return $x;
}
?>
From now on you can declare the type of data in your variables, and validate their values in a class
constructor.
See this example:
<?php
$y = & string("aaa");
// lets check, that $y is an object
var_dump($y);
// now we are overwritting $y variable with a scalar value of "zzz"
$y = "zzz";
// var_dump() shows us, that "zzz" is still an object, not the scalar type as in regular
PHP
var_dump($y);
// the line below should raise a fatal error, because "zzz" was a scalar type (string),
but it will be ok, because $y is still an object (thanks to autoboxing)
var_dump($y->toUpperCase());
?>
Now, the output is:
object(String)#1 (2) {
["value"]=>
string(3) "aaa"
["ref":protected]=>
int(1)
}
object(String)#2 (2) {
["value"]=>
string(3) "zzz"
["ref":protected]=>
int(1)
}
string(3) "ZZZ"
As you can see, PHP can be as good as Java or C#:)
To enforce strong data types you just have to do it in the wrapper class, like so:
<?php
class Integer extends AutoBoxedObject
{
public $value;
public function __construct($value) {
if(!is_int($value)) { throw new Exception("Invalid data type"); }
$this->value = $value;
}
public function getValue() {
return $this->value;
}
public function __toString() {
return (string)$this->value;
}
?>
}
----
Server IP: 69.147.83.197
Probable Submitter: 212.76.32.115
----
Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php
Edit -- https://master.php.net/note/edit/103463
Del: integrated -- https://master.php.net/note/delete/103463/integrated
Del: useless -- https://master.php.net/note/delete/103463/useless
Del: bad code -- https://master.php.net/note/delete/103463/bad+code
Del: spam -- https://master.php.net/note/delete/103463/spam
Del: non-english -- https://master.php.net/note/delete/103463/non-english
Del: in docs -- https://master.php.net/note/delete/103463/in+docs
Del: other reasons-- https://master.php.net/note/delete/103463
Reject -- https://master.php.net/note/reject/103463
Search -- https://master.php.net/manage/user-notes.php