ID:               47301
 User updated by:  jevgeni at geimanen dot com
 Reported By:      jevgeni at geimanen dot com
 Status:           Bogus
 Bug Type:         Feature/Change Request
 Operating System: Any
 PHP Version:      5.3.0beta1
 New Comment:

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?


Previous Comments:
------------------------------------------------------------------------

[2009-02-06 22:43:35] johan...@php.net

$a->intVar = 12 is overwriting the old intVar property, keeping the old
value would be a major beak to the way PHP works.

------------------------------------------------------------------------

[2009-02-04 11:00:07] 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.



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=47301&edit=1

Reply via email to