php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #47301 New magic method __setValue for REAL type checking.
Submitted: 2009-02-04 11:00 UTC Modified: 2009-02-09 12:37 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: jevgeni at geimanen dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.3.0beta1 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jevgeni at geimanen dot com
New email:
PHP Version: OS:

 

 [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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-02-06 22:43 UTC] johannes@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-09 12:37 UTC] jevgeni at geimanen dot com
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?
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 10:01:33 2025 UTC