Orm

bismark2005

Utente Attivo
8 Mar 2011
70
0
0
Salve ragazzi, ho il seguente codice Php:

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?
 
Non mi piace nemmeno un po' quella classe: è pericolosa e realizzata male.

PHP:
<?php
class User
{
    private $firstName;
    private $lastName;
    private $username;
    private $password;
    private $emailAddress;
    private $dateLastLogin;
    private $timeLastLogin;
    private $dateAccountCreated;
    private $timeAccountCreated;

    public function __call($method, array $arguments)
    {
        if (strpos($method, 'get') === 0) {
            $property = lcfirst(substr($method, 4));

            if (!isset($this->$property)) {
                throw $this->getBadMethodException($method);
            }

            return $this->$property;
        }

        if (strpos($method, 'set') === 0) {
            $property = lcfirst(substr($method, 4));

            if (!isset($this->$property)) {
                throw $this->getBadMethodException($method);
            }

            if (count($arguments) < 1) {
                throw $this->getBadArgumentsException($method, 1, count($arguments));
            }

            $this->$property = $arguments[0];

            return $this;
        }

        throw new BadMethodCallException(sprintf(
            'Call to undefined method %s::%s',
            __CLASS__,
            $method
        ));
    }

    private function getBadMethodException($method)
    {
        return new \BadMethodCallException(sprintf(
            'Call to undefined method %s::%s',
            __CLASS__,
            $method
        ));
    }

    private function getBadArgumentsException($method, $expectedArgs, $givenArgs)
    {
        return new \BadMethodCallException(sprintf(
            '%s::%s expects exactly %d arguments, %d given',
            __CLASS__,
            $method,
            $expectedArgs,
            $givenArgs
        ));
    }
}
 
Quella classe sta sul manuale Php 6 Guida per lo sviluppatore. E' solo la prima parte, poi prosegue e mette ulteriori cose. Comunque il capitolo 7 sull'ORM è davvero ostico non ci sto capendo nulla.

La spiegazione che ho dato è più o meno valida? Nel caso chiedo ulteriori delucidazioni, altrimenti non ci capisco nulla.

Hai qualche link dove l'orm sia spiegato bene?
 
Sì, la spiegazione è giusta, ma la classe mi pare insensata: usa eval() dunque presenta molti rischi di sicurezza e molte limitazioni (per esempio non è possibile settare valori diversi da stringhe o numeri), quando una simile tecnica è completamente priva di senso.
 

Discussioni simili