Estoy construyendo una clase con una serie de validaciones de entrada, y he decidido colocarlas dentro de un método __set (no estoy seguro de si esta es la forma correcta, ya que he limitado Experiencia OOP). Esto parece funcionar bien, arrojando los errores adecuados cuando se pasan valores no válidos desde fuera de la clase. Sin embargo, si se modifica una variable dentro de la clase, el método _ _set parece ignorarse por completo.PHP ignorando el método __set dentro de la clase
Cualquier idea sería apreciada enormemente
//RESULT:::::::::::::::::::::::::::::::
// PASS: Testing : hello
// PASS: Testing exception handling
// __SET: Setting b to 123
// PASS: Testing with valid value: 123
// FAIL: Testing exception handling World2
<?php
class Test {
public $a;
private $b;
function __set($key, $val) {
switch($key) {
case 'b':
if(!is_numeric($val)) throw new Exception("Variable $b must be numeric");
break;
}
echo ("__SET: Setting {$key} to {$val}<br/>");
$this->$key = $val;
}
function __get($key) { return $this->$key; }
function bMethod() {
$this->b = "World2";
}
}
$t = new Test();
//testing a
try {
$t->a = "hello";
echo "PASS: Testing $a: {$t->a}<br/>";
} catch(Exception $e) {
echo "FAIL: Testing $a";
}
//testing b
try {
$t->b = "world";
echo "FAIL: Testing $b exception handling<br/>";
} catch(Exception $e){
echo "PASS: Testing $b exception handling<br/>";
}
//testing b with valid value
try {
$t->b = 123;
echo "PASS: Testing $b with valid value: {$t->b}<br/>";
} catch(Exception $e) {
echo "FAIL: Testing $b";
}
//bypassing exception handling with method
try {
$t->bMethod("world");
echo "FAIL: Testing $b exception handling {$t->b}<br/>";
} catch(Exception $e) {
echo "PASS: Testing $b exception handling<br/>";
}
Esto me había foxed durante al menos una hora ... – Jonathan