Ciao a tutti,
come da titolo volevo sapere se fosse possibile serializzare solo una parte di un oggetto o meglio, nel mio caso, serializzarlo tutto tranne 2/3 proprietà.
Se si come?
Tnx.
Ciao a tutti,
come da titolo volevo sapere se fosse possibile serializzare solo una parte di un oggetto o meglio, nel mio caso, serializzarlo tutto tranne 2/3 proprietà.
Se si come?
Tnx.
dividi il codice prima e poi serializzi quello che vuoi... altrimenti non ci sono altre soluzioni.
http://php.net/manual/en/language.oop5.magic.php
serialize() checks if your class has a function with the magic name __sleep. If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return anything then NULL is serialized and E_NOTICE is issued.
VM su SSD da 5$! https://www.digitalocean.com/?refcode=f6925c7f0ddb
Ho provato ad usare __sleep da cui ritorno un array della sole proprietà che vorrei serializzare.
Solo che non riesco a ricreare l'oggetto nella pagina seguente, ovvero mi viene mostrato un errore dovuto al fatto che il metodo X è chiamato su un "non-object".
Why?
per curiosità, quando "deserializzi" l'oggetto, la classe a cui fa riferimento l'oggetto è stata già caricata?
http://www.php.net/manual/en/languag...ialization.php
se fai unIn order to be able to unserialize() an object, the class of that object needs to be defined. That is, if you have an object of class A and serialize this, you'll get a string that refers to class A and contains all values of variabled contained it. If you want to be able to unserialize this in another file, an object of class A, the definition of class A must be prest ent in in that file first. This can be done for example by storing the class definition of class A in an include file and including this file or making use of the spl_autoload_register() function.
che ti viene stampato a videO?Codice PHP:
var_dump(unserialize($stuff));
VM su SSD da 5$! https://www.digitalocean.com/?refcode=f6925c7f0ddb
1) Si la classe è ovviamente caricata, infatti senza __sleep il codice funziona.
2) Se faccio la deserializzazione dell'oggetto e provo a stamparlo a video con var_dump() ottengo un bool(false).
![]()
Ok ho capito dove sbagliavo.
Dovevo fare così:
Invece facevo cosìCodice PHP:
public function __sleep(){
return array('nome', 'peso', ... );
}
Grazie ancora.Codice PHP:
public function __sleep(){
return array($this->nome, $this->peso, ... );
}