|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-02-04 11:00 UTC] jevgeni at geimanen dot com
Description:
------------
It's a feature request for a new magic method named __setValue (could be other more suitable name)
There is already magic method named __set, which is called when inaccessible MEMBER is called. The one I propose should be called when Object is called, receive the value that this object is being set to, and return new value. For example:
class Int32 {
function __setValue($value) {
if (!is_int($value)) throw new Exception("Type checking...");
return $value;
}
}
class A {
public $intVar;
public function __construct() {
$this->intVar = new Int32();
}
}
$a = new A();
$a->intVar = 12; //is ok
$a->intVar = "asdf"; //throws exception
That feature would give TONES of new possibilities, type checking is one of them.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 22:00:01 2025 UTC |
So what you mean is that $a->intVar = 12 should look like $a->intVar = new Int32(12); or smth like that. But I don't see any problem here (except of one more check before overwriting the old intVar property). Here is fixed version of my __setValue magic method proposal: function __setValue($value) { if (!is_int($value)) throw new Exception("Type checking..."); return new Int32($value); } This will keep major beak untouched. If we assume that the old "overwriting" algorytm looks like this: 1. overwrite property value with $newValue Then the new one should look like this: 1. check if old value has magic method. 1.true. overwrite property value with oldValue.__setValue($newValue) 2.false. overwrite property value with $newValue Does it make any sense now?