Salve ragazzi, ho il seguente codice Php:
Quando viene richiamato il metodo setFirstName('Ed'); questo è un metodo che almeno apparentemente non esiste. Quindi nella classe userv1 viene “attivato” il metodo magico __call.
A questo metodo vengono passati 2 argomenti $strFunction e $arArguments che in realtà sono
$strFunction=setFirstName
$arArguments=Ed;
$strMethodType = substr($strFunction, 0, 3); ---->In pratica vengono assegnati alla variabile $strMethodType i primi 3 caratteri del metodo setFirstName quindi set.
Stessa cosa per $strMethodMember.
Poi abbiamo lo switch che a seconda se $strMethodType è set o get svolge il relativo codice.
Nel caso di set:
return($this->SetAccessor($strMethodMember, $arArguments[0]));
chiama e ritorna il metodo SetAccessor a cui vengono passati due valori ($strMethodMember, $arArguments[0]) che dovrebbero corrispondere a:
$strMethodMember=set
$arArgument[0]=questo dovrebbe corrispondere a ed.
Prima di proseguire con le domande e tralasciando eventuali errori grammaticali o di sintassi Php, va bene quanto ho detto?
Codice:
<?php
class User {
private $FirstName;
private $LastName;
private $Username;
private $Password;
private $EmailAddress;
private $DateLastLogin;
private $TimeLastLogin;
private $DateAccountCreated;
private $TimeAccountCreated;
public function __call($strFunction, $arArguments) {
$strMethodType = substr($strFunction, 0, 3);
$strMethodMember = substr($strFunction, 3);
switch ($strMethodType) {
case "set":
return($this->SetAccessor($strMethodMember,
$arArguments[0]));
break;
case "get":
return($this->GetAccessor($strMethodMember));
};
return(false);
}
private function SetAccessor($strMember, $strNewValue) {
if (property_exists($this, $strMember)) {
if (is_numeric($strNewValue)) {
eval('$this->' . $strMember . ' = ' . $strNewValue
. ';');
} else {
eval('$this->' . $strMember . ' = "' . $strNewValue
. '";');
};
} else {
return(false);
};
}
private function GetAccessor($strMember) {
if (property_exists($this, $strMember)) {
eval('$strRetVal = $this->' . $strMember . ';');
return($strRetVal);
} else {
return(false);
};
}
}
?>
Codice:
<?php
require ("userv1.php")
$objUser=new User();
$objUser->setFirstName("Ed");
$objUser->setLastName("Lecky");
$objUser->setUsername("ed");
print "First name is " .$objUser->getFirstName() . "<br />";
print "Last name is " . $objUser->getLastName(). "<br />";
print "Username is " . $objUser->getUsername(). "<br />";
?>
Quando viene richiamato il metodo setFirstName('Ed'); questo è un metodo che almeno apparentemente non esiste. Quindi nella classe userv1 viene “attivato” il metodo magico __call.
A questo metodo vengono passati 2 argomenti $strFunction e $arArguments che in realtà sono
$strFunction=setFirstName
$arArguments=Ed;
$strMethodType = substr($strFunction, 0, 3); ---->In pratica vengono assegnati alla variabile $strMethodType i primi 3 caratteri del metodo setFirstName quindi set.
Stessa cosa per $strMethodMember.
Poi abbiamo lo switch che a seconda se $strMethodType è set o get svolge il relativo codice.
Nel caso di set:
return($this->SetAccessor($strMethodMember, $arArguments[0]));
chiama e ritorna il metodo SetAccessor a cui vengono passati due valori ($strMethodMember, $arArguments[0]) che dovrebbero corrispondere a:
$strMethodMember=set
$arArgument[0]=questo dovrebbe corrispondere a ed.
Prima di proseguire con le domande e tralasciando eventuali errori grammaticali o di sintassi Php, va bene quanto ho detto?