Metodo Magico

bismark2005

Utente Attivo
8 Mar 2011
70
0
0
Salve ragazzi, non riesco a capire bene questo metodo:

Codice:
public function __set($propertyName, $value) {
            if(!array_key_exists($propertyName, $this->propertyTable)) {
                throw new Exception("Invalid property \"$propertyName\"!");
            }
            if(method_exists($this, 'set' . $propertyName)) {
                return call_user_func(
                                array($this, 'set' . $propertyName),
                                $value);
            } else {

                   
                if($this->propertyTable[$propertyName] != $value &&
                    !in_array($propertyName, $this->changedProperties)) {
                        $this->changedProperties[] = $propertyName;
                }
               
                $this->data[$this->propertyTable[$propertyName]] = $value;
            }
        }


Il metodo dovrebbe essere un metodo magico perchè inizia con il doppio underscore __ Questo metodo viene richiamato automaticamente quando viene usata in scrittura una proprietà inaccessibile.

Il metodo riceve 2 parametri ($propertyName e $value).

Codice:
 if(!array_key_exists($propertyName, $this->propertyTable)) {
                throw new Exception("Invalid property \"$propertyName\"!");
            }


array_key_exists cerca una chiave ($propertyName) nell'array $this->propertyTable. Se il risultato è true viene cambiato in false per via della negazione logica ! E quindi va ad eseguire l'altro if.


Codice:
if(method_exists($this, 'set' . $propertyName)) {
                return call_user_func(
                                array($this, 'set' . $propertyName),
                                $value);
            }


Questo controlla se il metodo $propertyName esiste nella classe $this. Se il risultato è true ritorna la funzione definita dall'utente array($this,'set'.$properttName) passandogli il parametro $value.

Non mi convince il significato di $this, a cosa si riferisce esattamente?

Grazie
 
$this si riferisce all'oggetto chiamato. Sistemandolo così:
PHP:
public function __set($propertyName, $value)
{
    if (!array_key_exists($propertyName, $this->propertyTable)) {
        throw new Exception(sprintf(
            'Invalid property "%s".',
            $propertyName
        ));
    }
    
    if (method_exists($this, $method = "set{$propertyName}")) {
        return call_user_func(array($this, $method), $value);
    }

    if (
        $this->propertyTable[$propertyName] != $value &&
        !in_array($propertyName, $this->changedProperties)
    ) {
        $this->changedProperties[] = $propertyName;
    }
   
    $this->data[$this->propertyTable[$propertyName]] = $value;
}
Deduciamo che, se la proprietà chiamata non è nell'array propertyTable, il metodo lancia un'eccezione.

Dopo il controllo, verifica se esiste un setter personalizzato per la proprietà, e in caso chiama quello.

A questo punto, se il valore in propertyTable è diverso da quello assegnato dall'utente (???) e la proprietà non è nell'array changedProperties, questa vi viene inserita.

Il nuovo valore viene inoltre inserito in data, usando come chiave il valore preso da propertyTable.

Alcuni passaggi non riesco a capirli... Dove hai preso quel codice? Così a occhio sembra una specie di ORM.
 
Grazie per avermi risposto. Allora il codice fa parte di questa classe:

Codice:
<?php
require_once('interface.Validator.php');

abstract class PropertyObject implements Validator {

    protected $propertyTable = array(); 
                                        
    protected $changedProperties = array(); 
                                          

    protected $data;                     
                                        

    protected $errors = array();         
                                        
    public function __construct($arData) {
        $this->data = $arData;
    }

    public function __get($propertyName) {
        if(!array_key_exists($propertyName, $this->propertyTable)) {
            throw new Exception("Invalid property \"$propertyName\"! ");
        }
        if(method_exists($this, 'get' . $propertyName)) {
            return call_user_func(
                   array($this, 'get' . $propertyName));
        } else {
            return $this->data[$this->propertyTable[$propertyName]];
        }
    }

    public function __set($propertyName, $value) {
        if(!array_key_exists($propertyName, $this->propertyTable)) {
            throw new Exception("Invalid property \"$propertyName\"!");
        }
        if(method_exists($this, 'set' . $propertyName)) {
            return call_user_func(
                            array($this, 'set' . $propertyName),
                            $value);
        } else {

            
            if($this->propertyTable[$propertyName] != $value && 
                !in_array($propertyName, $this->changedProperties)) {
                    $this->changedProperties[] = $propertyName;
            }
            //Now set the new value
            $this->data[$this->propertyTable[$propertyName]] = $value;
        }
    }

    public function validate() {
    
    }
}
?>

Mi leggo con calma la tua risposta e nel caso ti chiedo ulteriori cose.

Ciao e grazie
 

Discussioni simili