note 115249 added to jsonserializable.jsonserialize
| From: | dimstefan83 at gmail dot com | Date: | Fri, 20 Jun 2014 08:34:52 +0000 |
| Subject: | note 115249 added to jsonserializable.jsonserialize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199724@lists.php.net to get a copy of this message | ||
simple functions to make life easier.
I wanted a way to have a as property value the return value of a method in order to have it
initialized. example
class portItem implements JsonSerializable{
private $idPort;
private $location;
private $portLanguages;
public function getLocation() {
if(!isset($this->location)){
return new locationItem();
}
return $this->location;
}
public function setLocation($location) {
$this->location = $location;
}
public function setidPort($idPort){
$this->idPort=$idPort;
}
public function getidPort(){
if(!isset($this->idPort)){
return 0;
}
return intval($this->idPort);
}
public function setPortLanguages($portLanguages){
$this->portLanguages=$portLanguages;
}
public function getPortLanguages(){
if(!isset($this->portLanguages)){
return new genericItems();
}
return $this->portLanguages;
}
public function getCategoryLanguages(){
if(!isset($this->portLanguages)){
return new genericItems();
}
return $this->portLanguages;
}
public function getPortLanguageAtIndex($index=false){
if(!$index){
$index=0;
}
return $this->portLanguages[$index];
}
public function setPortLanguageAtIndex($index,$language){
$this->portLanguages[$index]=$language;
}
public function jsonSerialize() {
$dataConverter = new dataConverter();
return $dataConverter->convertToJson($this);
}
}
Wit 2 ways you can automatically convert for json.
1.by reading properties
public function convertToJson($object){
/*approch by using object properties*/
$class = get_class($this);
$json = array();
$properties = get_class_vars($class);
$keys = array_keys($properties);
$plength = count($keys);
for($i=0;$i<$plength;$i++){
$method = "get".$keys[$i];
if(method_exists ($this,$method)){
$json[$keys[$i]] = $this->$method();
}
}
return $json;
}
public function convertToJson($object){
/*approch by using object methods*/
$class = get_class($object);
$json = array();
$methods = get_class_methods($class);
$plength = count($methods);
$json = array();
for($i=0;$i<$plength;$i++){
if(stripos($methods[$i], "get")!==FALSE){
$property = mb_substr($methods[$i],
3,mb_strlen($methods[$i],'UTF-8'),'UTF-8');
$setter = "set".mb_substr($methods[$i],
3,mb_strlen($methods[$i],'UTF-8'),'UTF-8');
if(method_exists($object,$setter)){
$json[$property] = $object->$methods[$i]();
}
}
}
return $json;
}
By using this method the result will be a json with value for properties initialized according to
each method.
Things to note
1.if you are using the reading properties approach the code cant' work outside of the class for
private properties, because they are nto visible, you have to put it the code directly to public
function jsonSerialize() {} in order to work.
2.For both approaches has method seems not to be case sensitive else you are going to have problems,
i strongly suggest to pay attention in naming convention between property names and method names
idPort =>setIdPort() =>getIdPort() (i know my class is done not that way).
3.both approaches since you call method without knowing the arguments you need to pay attention in
arguments for getter methods where applicable, check my example
public function getPortLanguageAtIndex($index=false){
if(!$index){
$index=0;
}
return $this->portLanguages[$index];
}
that's why i have set it to optional argument. If you don't do that php will complain
about missing argument parameter in method XXX.
----
Server IP: 147.32.160.17
Probable Submitter: 178.59.96.212
----
Manual Page -- http://php.net/manual/en/jsonserializable.jsonserialize.php
Edit -- https://master.php.net/note/edit/115249
Del: integrated -- https://master.php.net/note/delete/115249/integrated
Del: useless -- https://master.php.net/note/delete/115249/useless
Del: bad code -- https://master.php.net/note/delete/115249/bad+code
Del: spam -- https://master.php.net/note/delete/115249/spam
Del: non-english -- https://master.php.net/note/delete/115249/non-english
Del: in docs -- https://master.php.net/note/delete/115249/in+docs
Del: other reasons-- https://master.php.net/note/delete/115249
Reject -- https://master.php.net/note/reject/115249
Search -- https://master.php.net/manage/user-notes.php